动态检查 onClickListener 中的复选框

Dynamically checking a checkbox in onClickListener

所以,我有一个复选框。当我选中此复选框时,我 不希望该框立即选中 。我弹出一个对话框,询问用户 "Mark as complete/incomplete" 或 "Cancel"。只有当用户选择 "Mark as complete/incomplete" 时,我才希望复选框切换。但是,每当我单击此选项时,对话框都会重新出现并且不会切换复选框。我不确定我在这里做错了什么。这是我的代码:

package com.example.testcheckbox;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends ActionBarActivity {

    CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cb = (CheckBox) findViewById(R.id.checkbox);
        cb.setChecked(true);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    final boolean isChecked) {

                CharSequence options[];

                if (isChecked) {
                    cb.setChecked(false);
                    options = new CharSequence[] { "Mark as Complete",
                            "Cancel" };
                } else {
                    cb.setChecked(true);
                    options = new CharSequence[] { "Mark as Incomplete", "Cancel" };
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case 0:
                                cb.toggle();
                        }

                    }
                });
                builder.show();
            }
        });
    }
}

这里是带有复选框的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testcheckbox.MainActivity" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

有什么想法吗?如果你不明白我想做什么,尽管问!谢谢

问题是,如果您将 CheckBox 的选中状态更改为 togglesetChecked,它会再次调用 CompoundButton.OnCheckedChangeListener

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;


public class MyActivity extends ActionBarActivity {

    private CheckBox cb;
    boolean isShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        cb = (CheckBox) findViewById(R.id.checkbox);
        cb.setChecked(true);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         final boolean isChecked) {
                if(!isShowing) {
                    isShowing = true;
                    CharSequence options[];
                    if (isChecked) {
                        cb.setChecked(false);
                        options = new CharSequence[]{"Mark as Complete",
                                "Cancel"};
                    } else {
                        cb.setChecked(true);
                        options = new CharSequence[]{"Mark as Incomplete", "Cancel"};
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            MyActivity.this);
                    builder.setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            switch (which) {
                                case 0:
                                    cb.toggle();
                                    dialog.cancel();
                                    break;
                                case 1:
                                    dialog.cancel();

                            }

                        }
                    });
                    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            isShowing = false;
                        }
                    });
                    builder.show();
                }
            }
        });
    }
}

请试试这个,

 package com.example.testcheckbox;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends ActionBarActivity {

    CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cb = (CheckBox) findViewById(R.id.checkbox);
    cb.setChecked(true);
    cb.setOnCheckedChangeListener(mOnCheckedChangeListener);
    }

    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {

        CharSequence options[];

        if (isChecked) {
        cb.setChecked(false);
        options = new CharSequence[] { "Mark as Complete", "Cancel" };
        } else {
        cb.setChecked(true);
        options = new CharSequence[] { "Mark as Incomplete", "Cancel" };
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case 0: {
            cb.setOnCheckedChangeListener(null);
            cb.toggle();
            cb.setOnCheckedChangeListener(mOnCheckedChangeListener);
            }
            }

        }
        });
        builder.show();
    }
    };
}