如果在java中取消选中该复选框,如何禁用正负按钮?

时间:2014-08-22 09:36:09

标签: java android eclipse popup alertdialog

如果取消选中该复选框,我会尝试通过禁用确定和取消按钮来执行提醒消息。

reconfirm.java:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
                LayoutInflater layoutInflater 
                 = (LayoutInflater)getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE);  
                View popupView = layoutInflater.inflate(R.layout.popup, null);


                alertDialogBuilder.setView(popupView);

                CheckBox check= (CheckBox)findViewById(R.id.checkBox1);  

            if (check.isChecked() ) {

                    AlertDialog dialog = null;
                    ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
                }




                alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {

                          Intent intObj = new Intent(getApplicationContext(),
                                agree.class);
                        startActivity(intObj);
                      }
                  });


                alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {


                     Intent intObj = new Intent(getApplicationContext(),
                                IntentExampleActivity.class);
                            startActivity(intObj);


                  }

                  });

                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

3 个答案:

答案 0 :(得分:0)

尝试使用setEnabled()setClickable方法获取按钮。

这是doc

答案 1 :(得分:0)

尝试以下代码段。希望你能从中得到一些想法:)

if (check.isChecked()) {
 alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
 alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}

其中dialog是AlertDialog的对象。

答案 2 :(得分:0)

这已在此处得到解答 - How to disable / enable dialog negative positive buttons?

在dialog.show之后使用下面的代码

如果(your_condition_true)     dialog.getButton(AlertDialog.BUTTON1).setEnabled(假); // BUTTON1是正按钮