Android 复选框始终处于未选中状态
在下面的代码中,我尝试使用一个复选框。但在运行时,它是不可单击的,我的意思是当我单击复选框时,不会发生任何事情或更改,并且复选框始终保持未选中状态 为什么会这样 代码:Android 复选框始终处于未选中状态,android,checkbox,Android,Checkbox,在下面的代码中,我尝试使用一个复选框。但在运行时,它是不可单击的,我的意思是当我单击复选框时,不会发生任何事情或更改,并且复选框始终保持未选中状态 为什么会这样 代码: mcbComputations.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mcbComputations.isChecked()) {
mcbComputations.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mcbComputations.isChecked()) {
mATComputations = new ATComputations();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mATComputations.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mATComputations.execute();
}
} else {
mATComputations.cancel(true);
}
使用
而不是
setOnClickListener
对于
复选框
在处理复选框时,请使用onCheckChangedListener
:
mcbComputations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mcbComputations.isChecked()) {
mATComputations = new ATComputations();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mATComputations.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mATComputations.execute();
}
} else {
mATComputations.cancel(true);
}
}
});
mcbcomputions
代表CheckBox
?@PiyushGupta是的,一旦checkchanged就应该用来捕获选中状态。我认为当覆盖某个复选框的click listener时,你应该调用它的超级实现来选中/取消选中你的复选框,手动选中/取消选中你的复选框。
mcbComputations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mcbComputations.isChecked()) {
mATComputations = new ATComputations();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mATComputations.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mATComputations.execute();
}
} else {
mATComputations.cancel(true);
}
}
});