Android CheckBox onClickListener 不起作用

标签 android checkbox

可能是这个重复的问题,但我没有找到适合我的答案。

CheckBoxClickListener 不工作。

这个 xml:

<CheckBox
            android:id="@+id/checkbox_visibility"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/mat_card_padding"/>

Activity 中的这段代码:

        mVisibilityCheckBox = (CheckBox) findViewById(R.id.checkbox_visibility);
        mVisibilityCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // this method doesn't call
                    Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
                }
            });

UPD 我从答案中添加代码 - 但这对我不起作用(

mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // this doesn't work
                Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
            }

        }
    });

最佳答案

mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
           Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
        }

    }
});

关于Android CheckBox onClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752168/

相关文章:

javascript - 我的 tablesorter 中的列排序覆盖了我的表头中的 checkall 复选框

css - 不同的 Chrome 复选框样式

android - Android 平板电脑的 IMEI?

Debug模式下的 Android Studio "view bitmap"不显示位图

android - 将样式应用于 Android Gallery 中的项目

java - LibGdx 在 Android Studio 中有多个错误

安卓 OSM : How to replace default current location overlay icon

JavaScript - 使用 "checked/unchecked checkbox"作为 "IF ELSE condition statement"

javascript - 从复选框向 Codemirror 添加值/从 Codemirror 中删除值

javascript - 仅通过单选来 react 复选框功能