简体   繁体   English

如果在Java中未选中复选框,如何禁用正负按钮?

[英]How to disable positive and negative button if the checkbox is unchecked in java?

I'm trying to do alert message by disable ok and cancel button if the checkbox is unchecked. 我正在尝试通过禁用“确定”和“取消”按钮来发出警报消息,如果未选中该复选框。

reconfirm.java: 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();

Try Using setEnabled() and setClickable method for buttons. 尝试对按钮使用setEnabled()setClickable方法。

here is the doc 这是医生

Try following code snippets. 尝试遵循以下代码片段。 Hope you get some idea from this :) 希望你从中得到一些想法:)

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

where dialog is object of AlertDialog. 其中dialog是AlertDialog的对象。

This is answered already here-- How to disable / enable dialog negative positive buttons? 这已经在这里得到解答- 如何禁用/启用对话框的负面肯定按钮?

after dialog.show use below code 在dialog.show之后使用以下代码

if(your_condition_true) dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); if(您的条件_true)dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button // BUTTON1是肯定按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM