动态检查onClickListener中的复选框

时间:2015-01-05 20:41:36

标签: java android checkbox android-checkbox

所以,我有一个复选框。当我选中此复选框时,我不希望该框立即检查。我弹出一个对话框,询问用户“标记为完整/不完整”或“取消”。只有当用户选择“标记为完整/不完整”时,我才希望复选框切换。但是,每当我单击此选项时,对话框将重新出现,并且不会切换复选框。我不确定我在这里做错了什么。这是我的代码:

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>

有什么想法吗?如果你不明白我想做什么,那就问问吧!谢谢

2 个答案:

答案 0 :(得分:1)

问题在于,如果您通过CheckBox更改toggle的已选中状态,或setChecked再次调用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();
                }
            }
        });
    }
}

答案 1 :(得分:1)

请试试这个,

 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();
    }
    };
}