简体   繁体   中英

How to detect touch events on disabled checkbox in android

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM