无法关闭自定义对话框

发布于 2024-11-16 02:32:44 字数 955 浏览 4 评论 0原文

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃→可爱长大的 2024-11-23 02:32:44

首先,您的对话框需要位于类的范围内,因此您需要

public Dialog dialog;

在任何方法之外进行声明。然后,在您的 onCreate() 方法中,像您已有的那样创建对话框。仅保留对 OnClickListeners 的 show()dismiss() 调用。

然后,您的按钮将如下所示:

Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.show();
    }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.dismiss();
    }
});

另外,最好提出一个适合您的命名约定,而不是随机大写或不大写资源名称(例如,Button03 与 Button1);

First, your Dialog needs to be in the scope of your class, so you need to declare

public Dialog dialog;

outside of any methods. Then, in your onCreate() method, create the Dialog like you already have. Leave just the show() and dismiss() calls to the OnClickListeners.

Your buttons would then look like:

Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.show();
    }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.dismiss();
    }
});

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);

指尖上得阳光 2024-11-23 02:32:44

您还可以为这两个按钮编写一个通用的 onClick,如下所示。这样就避免了每个按钮都重写onClick事件,维护起来很方便。

公共无效onClick(查看v){

   switch(v.getId())
   {
       case R.id.button3:
           dialog.show();

       case R.id.Button01:
           dialog.dismiss();
   }


}

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) {

   switch(v.getId())
   {
       case R.id.button3:
           dialog.show();

       case R.id.Button01:
           dialog.dismiss();
   }


}
一指流沙 2024-11-23 02:32:44

OnClickListener 外部初始化对话框。

Dialog dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.maindialog);

button3.setOnClickListener(new OnClickListener() {           
       public void onClick(View v) {              
         dialog.show();    
       }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
          dialog.dismiss();
       }
});

Initialize the Dialog outside the OnClickListener.

Dialog dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.maindialog);

button3.setOnClickListener(new OnClickListener() {           
       public void onClick(View v) {              
         dialog.show();    
       }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
          dialog.dismiss();
       }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文