问题描述
我正在测试AlertDialog的行为以集成到更大的组件中.我无法再次显示相同的对话框.这是测试代码:
I was testing the behavior of AlertDialog to integrate in a bigger component. I am unable to show the same dialog again. Here is the test code:
public class MainActivity extends AppCompatActivity {
private AlertDialog alertDialogACCreationRetry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertDialogACCreationRetry = new AlertDialog.Builder(this)
.setTitle("Account creation failed")
.setMessage("There was a problem connecting to the Network. Check your connection.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialogACCreationRetry.show();
alertDialogACCreationRetry.show();
}
}
我尝试将alertDialogACCreationRetry.show();
放在重试"按钮内,但它仍然不会显示.我也尝试过将alertDialogACCreationRetry.dismiss();
放在重试"按钮内,然后在外部调用alertDialogACCreationRetry.show();
,它仍然没有显示.更令人恐惧的是,如果不应该允许的话,它不会给我重新显示它的异常.
I have tried putting the alertDialogACCreationRetry.show();
inside the Retry button but it still won't show. I have also tried putting alertDialogACCreationRetry.dismiss();
inside the Retry button and then calling alertDialogACCreationRetry.show();
outside, it still doesn't show. More so it is frightening that it doesn't give me an exception on reshowing it if that is not supposed to be allowed.
所以,我的问题是:一旦按下按钮(自动)将其关闭后,我是否每次都要创建一个新对话框?
推荐答案
public void showAlertDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Time");
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
// call function show alert dialog again
showAlertDialog();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
这篇关于再次显示相同的AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!