Android:警告对话框中的按钮依赖于复选框

时间:2014-03-20 09:10:27

标签: android button checkbox dialog

我的活动以警报对话框提示开始。我有一个复选框,请勿再显示此信息。我想要按钮 Checkbox之后可单击 点击,Thnx提前,我希望我的话让你明白我在寻找什么。这是我的代码:

    @Override
    protected void onResume() {
        AlertDialog.Builder adb = new     AlertDialog.Builder(this);
        LayoutInflater adbInflater =     LayoutInflater.from(this);
        View eulaLayout = adbInflater.inflate(R.layout.note_for_layout, null);
    final CheckBox dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);

        adb.setCancelable(false); 
        adb.setView(eulaLayout);
        adb.setTitle("Warning!");
         adb.setMessage("");
  adb.setIcon(R.drawable.alert1);
    adb.setPositiveButton("Accept",     new DialogInterface.OnClickListener() {
                public void     onClick(DialogInterface dialog, int which) {
                   String checkBoxResult =     "NOT checked";
                    if (dontShowAgain.    isChecked())
                        checkBoxResult =     "checked";
                    String     PREFS_NAME = null;
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                    editor.putString("skipMessage", checkBoxResult);

                editor.commit();

                    return;
            }
        }); 

        adb.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void     onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";
                    String PREFS_NAME = null;
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                // Commit the edits!
                editor.commit();
                    finish();
            }
        });
        String PREFS_NAME = null;
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String skipMessage = settings.getString("skipMessage", "NOT checked");
    if (!skipMessage.equals("checked"))
        adb.show();

        super.onResume();

2 个答案:

答案 0 :(得分:0)

您是否希望在单击复选框后可以单击该按钮?如果是这样,那么你可以这样做:

final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);

然后在Checkbox中添加一个监听器:

dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if (isChecked){
            // perform logic
            positiveButton.setEnabled(true);
        }
    }

});

答案 1 :(得分:0)

您可以使用此代码启用或禁用该按钮。

dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true/false);