[英]Showing the same AlertDialog again
我正在测试AlertDialog的行为以集成到更大的组件中。 我无法再显示相同的对话框。 这是测试代码:
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();
在Retry按钮内,然后调用alertDialogACCreationRetry.show();
外面,它仍然没有显示。 更令人恐惧的是,如果不允许这样做,它就不会给我一个例外。
所以,我的问题是: 每次按下按钮后(自动)解除后 ,我是否必须每次创建一个新的Dialog?
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();
}
可能发生的是你在解除对话之前调用.show(),所以这是一个无操作。 从重试按钮侦听器返回时,对话框将被关闭。
PS这似乎与我的建议一致: https : //stackoverflow.com/a/12110191/3286819
为了使这项工作,您应该在网络代码真正重试该操作后再次调用该节目,可能是在后台线程或类似的东西中。
最后,另一方面,简单地再次创建对话框更有意义,因为在某些情况下保持相同的对话框可能会造成泄漏。
您无需重新创建alertDialogACCreationRetry
。 您可以重复使用它,但不能同时显示同一对象的多个警报对话框。 show()
方法在内部检查当前警告对话框是否已经显示。 如果它显示然后它将返回不执行任何其他新对话框将显示。
因此,请确保在对话框中调用show()
之前调用dismiss()
这就是我尝试过的
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
getActivity().getSupportFragmentManager();
alertDialogACCreationRetry = new AlertDialog.Builder(getActivity())
.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();
View view = inflater.inflate(R.layout.frag1, container, false);
TextView viewById = (TextView) view.findViewById(R.id.frag1_text);
viewById.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
alertDialogACCreationRetry.show();
}
});
return view;
}
在上面的alertDialogACCreationRetry.show()
调用内部onClick()
将显示对话框,如果没有显示对话框,否则它什么都不做。
你可以这样做:
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.getButton(AlertDialog.BUTTON_POSITIVE).
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean yourLogicGoFine = false;
if (yourLogicGoFine){
alertDialogACCreationRetry.dismiss();
}else{
Toast.makeText(finalizarActivity, "Something was wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
祝好运!
正如其他人所说,调用show()的原因并没有使对话框显示是因为它还没有被解雇。 该方法通过设置对话框的可见性来工作,而不是添加另一个对话框,因此您肯定无法获得同一对话框的2层。
您可以添加一个按钮来触发显示对话框,以确保对话框完全被解除。 像这样:
findViewById(R.id.show_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialogACCreationRetry.show();
}
});
关闭对话框后单击按钮。 它肯定会重新出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.