[英]Android CheckBox onClickListener doesn't work
May be this duplicate question but I didn't find answer for me. 可能是这个重复的问题,但我没有找到答案。
My ClickListener
for CheckBox
doesn't work. 我的
CheckBox
ClickListener
不起作用。
This xml: 这个xml:
<CheckBox
android:id="@+id/checkbox_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/mat_card_padding"/>
This code from Activity: 来自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 I add code from answer - but this doesn't work for me( 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();
}
}
});
check this example.this could help you. 检查这个例子。这可以帮到你。 http://www.bipinrupadiya.com/2013/08/android-checkbox-setoncheckedchangelist.html
http://www.bipinrupadiya.com/2013/08/android-checkbox-setoncheckedchangelist.html
Edited: the code required here is shown below: 编辑:此处所需的代码如下所示:
CheckBox hin = (CheckBox) findViewById(R.id.chkHindi);
hin.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
Add this code and it will work 添加此代码,它将工作
mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mVisibilityCheckBox.isChecked()) {
// this doesn't work
Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
}
}
});
Hope this helps 希望这可以帮助
mVisibilityCheckBox.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(satView.isChecked()){
System.out.println("Checked");
}else{
System.out.println("Un-Checked");
}
}
});
Simply add clickable="true"
in CheckBox xml. 只需在CheckBox xml中添加
clickable="true"
。
For example : 例如 :
<CheckBox
android:id="@+id/checkbox_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/mat_card_padding"
android:clickable="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.