java - 动态检查 onClickListener 中的复选框

标签 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>

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

最佳答案

请尝试这个,

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

关于java - 动态检查 onClickListener 中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27787543/

相关文章:

android - 无法通过 Visual Studio Team Services 对 Xamarin Android APK 进行签名

java - 如何在 Android 中重置 AtomicInteger 唯一 ID?

java - HttpUrlConnection 与发布请求和参数作为 JSON 对象?

java - init() : Couldn't get lock for/var/lib/tomcat6/./tm.out 中的 Atomikos 错误

java - 创建和使用包含非文本资源的 jar

jquery - 选中/取消选中后的 CSS 动画

jquery - 在旁边的框中放置复选标记后,希望文本变为粗体

java - JBoss JMS : ERROR: JBREM000200: Remote connection failed: javax. security.sasl.SaslException:身份验证失败:服务器出现

java - 当在登录 Activity 中按下退格键时,Android 在登录屏幕上关闭

javascript - 通过 <a href> 发送复选框数据