Android - 警告对话框 - 自制取消按钮

标签 android android-alertdialog onclicklistener

我已经使用自己的布局 xml 更新了警报对话框格式。

在这个自定义的提醒对话框中有2个按钮,一个是保存正在输入的数据,另一个是取消按钮。

我如何编写取消按钮,以便当用户单击它时,只是简单地关闭对话框?

   public OnClickListener NewRowButtonListener = new OnClickListener()
   {
      @Override
      public void onClick(View v) 
      {               
          AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
          builder.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null));
          builder.create();

          AlertDialog Custom_dialog_add = builder.create();
          Custom_dialog_add.show(); // show the Dialog

          Button CancelButton = (Button) findViewById(R.id.CancelButton);
          CancelButton.setOnClickListener(new View.OnClickListener() {       
              @Override 
              public void onClick(View v) {Custom_dialog_add.cancel();}  //WRONG: Cannot refer to a non-final variable Custom_dialog_add inside an inner class defined in a different method
            });         
      } 
   };  

现修改如下:

       public OnClickListener NewRowButtonListener = new OnClickListener() 
       { 
          @Override 
          public void onClick(View v)  
          { 
              AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); 
              dialog.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null)); 
              dialog.create(); 

               final AlertDialog test = dialog.create(); 
               test.show();

               Button close = (Button) findViewById(R.id.CancelButton); 
               close.setOnClickListener(new android.view.View.OnClickListener() { 
                   public void onClick(View v) { 
                       test.dismiss(); 
                   } 
               });
          }
       };

Eclipse 修改后的代码没有报BUG,但是在模拟的时候,跑出了空指针异常。日志如下。怎么解决?

09-28 20:15:19.505: E/AndroidRuntime(25847): FATAL EXCEPTION: main
09-28 20:15:19.505: E/AndroidRuntime(25847): java.lang.NullPointerException
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.pearappx.gamescore3.MainActivity$4.onClick(MainActivity.java:422)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.view.View.performClick(View.java:3627)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.view.View$PerformClick.run(View.java:14329)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Handler.handleCallback(Handler.java:605)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Looper.loop(Looper.java:137)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.app.ActivityThread.main(ActivityThread.java:4511)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

这段代码对您有帮助吗?

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_layout, null);
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("dialog");
        dialog.setView(view);
        final AlertDialog test = dialog.create();


        Button close = (Button) view.findViewById(R.id.close_button);
        close.setOnClickListener(new android.view.View.OnClickListener() {
            public void onClick(View v) {
                test.dismiss();

            }
        });

编辑更新版本:

    //Create new alert dialog
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    //set title
    dialog.setTitle("title");
    //create the dialog in a final context
    final AlertDialog test = dialog.create();
    //inflate the custom layout in to a View object
    View view = getLayoutInflater().inflate(R.layout.custom_dialog_add, null);

    //find the Button object within the inflated view
    //                       ↓↓↓
    Button close = (Button) view.findViewById(R.id.CancelButton);
    //set the onClickListener
    close.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
            test.dismiss(); 
        } 
    });
    //show the dialog
    test.show();

不要忘记使用正确的导入!

关于Android - 警告对话框 - 自制取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12588427/

相关文章:

java - 如何通过 Onclicklistener 移动到 json 数组中的下一个 json 项目

java - 使用TextView的onClick转到带有fragment的下一页

android - 如何通过代码圆化 AlertDialog 的角

android - 警报对话框生成器 : No items for checkboxes are shown

android - 选择微调项时应用程序崩溃?

javascript - 切换当前元素的可见性

java - 我如何解决在 android :onClick attribute? 的父或祖先上下文中找不到方法

android - 如何在 android mpchart 中获取 Y 轴标签?

android - 使用 Jetpack 的 Android 导航组件销毁/重新创建的 fragment

Android 应用程序仅在 Eclipse 调试时因 SIGABRT Signal 6 而崩溃