【问题标题】:Dynamically checking a checkbox in onClickListener动态检查 onClickListener 中的复选框
【发布时间】:2015-01-06 05:13:58
【问题描述】:

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

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>

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

【问题讨论】:

  • cb.toggle();将调用 OnCheckedChangeListener 这就是警报重新出现的原因。因此在此期间您必须将 OnCheckedChangeListener 设置为 null。并在此之后重置。
  • @Krish 有道理,关于如何将监听器设置为 null 并重置它有什么帮助吗?

标签: java android checkbox android-checkbox


【解决方案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();
    }
    };
}

【讨论】:

    【解决方案2】:

    问题是,如果您将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();
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 这似乎只适用于 CheckBox 的第一次检查。如果您尝试重新检查,对话框不会显示
    猜你喜欢
    • 2018-10-14
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多