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

时间:2012-09-25 17:51:12

标签: android alertdialog onclicklistener

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

在这个定制的警告对话框中有2个按钮,一个按钮用于保存正在输入的数据,另一个按钮用于取消按钮。

我如何为CANCEL按钮编写,以便当用户点击它时,只是简单地放弃对话框?

   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没有报告修改过的编码中的错误,但是在模拟时,它会耗尽一个nullpointer异常。 logcat如下。怎么会这样解决?

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)

5 个答案:

答案 0 :(得分:2)

此代码段会帮助您吗?

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

不要忘记使用正确的进口!

答案 1 :(得分:0)

您是否正在使用单独的活动来将布局设置为对话框或任何其他方式? 如果您使用单独的活动来实现布局

只需致电

youractivity.this.finish()

在取消按钮onclick事件

答案 2 :(得分:0)

这里有一个例子:

 // creates Dialogs for this Activity
   @Override
   protected Dialog onCreateDialog(int id) {
       final Dialog dialog;
       switch(id) {
       case DIALOG_REALLY_EXIT_ID:
           dialog = new AlertDialog.Builder(this).setMessage(
                               "Do you really want to exit this activity?")
           .setTitle("Exit activity")                    
           .setCancelable(false)
           .setPositiveButton("Yes",
                   new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                //add your code you would like to be execute when clicking "yes"
                //for example the below to exit your activity
                    //Main.this.finish();
               }
           })
           .setNegativeButton("No",
                   new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel(); //to dismiss this dialog
           //add any additional things you would like to execute when pressing the no button

               }
           }).create();
           break;
       default:
           dialog = null;
       }
       return dialog;
   }

答案 3 :(得分:0)

这在对话框{}内(例如:public void Dialog(...){here ..}

    DialogInterface.OnClickListener cancel = new DialogInterface.OnClickListener() {            
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            1 add = 1.this;
            add.finish();

            Intent showActivity = new Intent(1.this, 2.class);
            1.this.startActivity(showActivity);

        }
    };

编辑这些:
1 - 您所在的.java文件的名称。
2 - 取消Dialog时要打开/显示的.java类的名称。

然后把这段代码

builder.setNegativeButton("Cancel", cancel);

会发生什么? 当您单击“取消”按钮时,它将关闭“页面(带对话框)并打开您想要的活动/页面。


或试试这个:dialogo.setNeutralButton("Cancel", null);

答案 4 :(得分:0)

您可以自己创建按钮。在该按钮上设置onClickListener,在onClick()内部,您可以调用dialog.cancel();来取消对话框。