如果在java中未选中复选框,如何禁用“正”和“负”按钮?

如果在java中未选中复选框,如何禁用“正”和“负”按钮?,java,android,eclipse,popup,android-alertdialog,Java,Android,Eclipse,Popup,Android Alertdialog,如果复选框未选中,我尝试通过禁用“确定”和“取消”按钮来发送警报消息 reconfirm.java: AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this); LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()

如果复选框未选中,我尝试通过禁用“确定”和“取消”按钮来发送警报消息

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();
尝试对按钮使用
setEnabled()
setClickable
方法


以下是

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


其中dialog是AlertDialog的对象。

这里已经回答了这个问题--

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

如果(您的情况为真)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false)//按钮1为正按钮

请详细说明您的需要?@deniz当我点击复选框“I agre…”时,确定和取消将启用。在此之前,按钮禁用。它仅适用于API 14。我使用的是API 10。使用AlertDialog=alertDialogBuilder.create()和使用AlertDialog=alertDialogBuilder.create()有什么不同;最终AlertDialog=builder.create()@user3714182我已经更新了我的答案。我的目的是告诉你它是AlertDialog的对象。我刚刚回答了你的代码。它对你有用吗?没有。我正在写。setEnabled(false);在alertDialog.show()之后。。。没有显示错误,但在设备中显示“不幸的是,您的应用程序已停止”您可以打印您的日志吗?请编写一个复选框侦听器(setOnCheckedChangeListener)。在它的if(isChecked){dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);}else{dialog.getButton(AlertDialog.BUTTON1).setEnabled(true);}
if (check.isChecked()) {
 alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
 alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}