无法关闭自定义对话框
//About Button in the principal menu
final Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//set up dialog
Dialog dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("About");
dialog.setCancelable(true);
//now that the dialog is set up, it's time to show it
dialog.show();
Button closeButton = (Button) dialog.findViewById(R.id.Button01);
// closeButton.setOnClickListener(new Button.OnClickListener() {
// public void onClick(View view) {
// dialog.dismiss();
// }
// });
if(v==closeButton)
dialog.dismiss();
}
});
我有这段代码,但解雇不起作用。
我有一个“关于”按钮,当我单击它时会显示对话框窗口。 然后对话框窗口有一个“确定”按钮,此“确定”按钮应该关闭对话框,但关闭不起作用。 你能帮我知道为什么吗?
//About Button in the principal menu
final Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//set up dialog
Dialog dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("About");
dialog.setCancelable(true);
//now that the dialog is set up, it's time to show it
dialog.show();
Button closeButton = (Button) dialog.findViewById(R.id.Button01);
// closeButton.setOnClickListener(new Button.OnClickListener() {
// public void onClick(View view) {
// dialog.dismiss();
// }
// });
if(v==closeButton)
dialog.dismiss();
}
});
I have this code but the dismiss is not working.
I have an "about" button and when i click on in it shows the dialog window.
Then the dialog windows has a "OK" button and this OK button should dismiss the dialog but the dismiss is not working.
Could you help me to know why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您的对话框需要位于类的范围内,因此您需要
在任何方法之外进行声明。然后,在您的
onCreate()
方法中,像您已有的那样创建对话框。仅保留对 OnClickListeners 的show()
和dismiss()
调用。然后,您的按钮将如下所示:
另外,最好提出一个适合您的命名约定,而不是随机大写或不大写资源名称(例如,Button03 与 Button1);
First, your Dialog needs to be in the scope of your class, so you need to declare
outside of any methods. Then, in your
onCreate()
method, create the Dialog like you already have. Leave just theshow()
anddismiss()
calls to the OnClickListeners.Your buttons would then look like:
Also, it's good to come up with a naming convention that works for you rather than randomly capitalizing or not capitalizing resource names (e.g., Button03 vs. button1);
您还可以为这两个按钮编写一个通用的 onClick,如下所示。这样就避免了每个按钮都重写onClick事件,维护起来很方便。
公共无效onClick(查看v){
You can also write a common onClick for both the buttons like this. This will avoid the rewriting of onClick event for every button.It will become easy for maintenance.
public void onClick(View v) {
在
OnClickListener
外部初始化对话框。Initialize the Dialog outside the
OnClickListener
.