How to detect touch events on disabled checkbox in android

扶醉桌前 提交于 2019-12-22 14:00:25

问题


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.

The onTouchListener as well as the onClickListener doesn't seem to be called when the checkbox is set to ENABLED=false. Can anyone help?


回答1:


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.

What I have done is a checkbox with white text color but beginning 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"

In color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="white">#FFFFFF</color>
   <color name="grey">#808080</color>
</resources>

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" />

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;
   }
});

Hope this helps you




回答2:


Usually the behavior for a situation like that is a question-mark in Android. One thing you may be able to do is to put the CheckBox from something descended from ViewGroup (One of the many layouts like FrameLayout, for example) and use setOnClickListener on it.



来源:https://stackoverflow.com/questions/14158317/how-to-detect-touch-events-on-disabled-checkbox-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!