我想在按下按钮时保持对话框打开。
目前正在关闭。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
是的你可以。您基本上需要:
- 创建对话框对话生成器
- show() 对话框
- 找到显示的对话框中的按钮并覆盖它们
onClickListener
因此,创建一个监听器类:
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If you want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
然后在显示对话框时使用:
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
请记住,您需要显示对话框,否则将找不到该按钮。另外,一定要改变DialogInterface.BUTTON_POSITIVE为您用于添加按钮的任何值。另请注意,在添加按钮时对话生成器你需要提供onClickListeners
- 但是,您不能在其中添加自定义侦听器 - 如果您之后不覆盖侦听器,对话框仍然会关闭show()
叫做。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)