如何检测触摸事件在Android中的禁用复选框 [英] How to detect touch events on disabled checkbox in android
问题描述
我有一个布局
带有复选框。当一个复选框被选中时,其他复选框设置为 CHECKED = true
和 ENABLED = false
。现在我想让用户能够点击任何禁用的复选框,如果他这样做,一个设置为启用和选中,所有其他都禁用。
I have a layout
with couple checkboxes. When one checkbox gets checked the others are set to CHECKED=true
and ENABLED=false
. Now I want the user to be able to tap on any of that disabled checkboxes and if he does that, one is set to enabled and checked and all the other are disabled.
当复选框设置为<$ c时, onTouchListener
以及 onClickListener
$ c> ENABLED = false 。任何人都可以帮助?
The onTouchListener
as well as the onClickListener
doesn't seem to be called when the checkbox is set to ENABLED=false
. Can anyone help?
推荐答案
您无法在禁用复选框上接收活动。如果将禁用复选框置于布局(如Framelayout)上,则可以在单击布局时接收事件,但不在禁用复选框中。
You can not receive events on a disabled checkbox. If you put the disabled checkbox on a layout like Framelayout, you can receive the events when you click on the layout but not in the disabled checkbox. The best way if you want to capture events on a disabled checkbox is to simulate a disabled checkbox simply, and capture a long click event to activate again, for example.
如果您想要在禁用复选框上捕获事件,最好的方法是简单地模拟一个禁用的复选框,并捕获一个长时间点击事件。我已经做了一个复选框与白色文本颜色,但开始与灰色文本颜色和未选中,与一个布尔停止变量,你在每个onCheckedChanged方法之前检查。除非您更改布尔值停止变量,否则不会检查复选框。你可以按多个复选框,如果你想要什么都没有发生。它只是看起来被禁用,但当你长按一下你解除阻塞布尔停止变量,并将灰色文本颜色复选框变为白色像一个普通的复选框。
What I have done is a checkbox with white text color but begining with grey text color and unchecked, with a boolean stopper variable which you check before on every onCheckedChanged method. Checkbox is never checked unless you change the boolean stopper variable. You can press on checkbox many times as you want and nothing happens. It only appears to be disabled but when you press a long click you unblock the boolean stopper variable and change the grey text color checkbox to white like a normal checkbox. You can change the stopper variable when you want and "disabling it again"
在color.xml中:
In color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="grey">#808080</color>
</resources>
在main.xml中:
In main.xml:
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="text" />
在main.java代码中,onCreate方法:
In the main.java code, onCreate method:
//define a boolean stopper variable to check on event
boolean chkActivated = false;
checkbox = (CheckBox) findViewById(R.id.checkbox1);
checkbox.setTextColor(getResources().getColorStateList(R.color.grey));
checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(chkActivated){
if (isChecked) {
//Do everthing you want when checked
}else{
//Do everthing you want when unchecked
}
}else{
checkbox.setChecked(false);
Toast.makeText(Activity.this, "It is disabled. to activate press long click"), Toast.LENGTH_SHORT).show();
}
}
});
checkbox.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
chkActivated = true;
checkbox.setTextColor(getResources().getColorStateList(R.color.white));
checkbox.setChecked(true);
return true;
}
});
希望这可以帮助你
这篇关于如何检测触摸事件在Android中的禁用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!