可能是这个重复的问题,但我没有找到适合我的答案。
CheckBox
的ClickListener
不工作。
这个 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/