Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a CheckBox from being checked?

Tags:

android

I would like to be able to prevent a CheckBox from being selected (or to set it back to unselected), when the CheckBox is clicked

How can I achieve this?

I do not want to simply disable the checkbox. I want the user to think it is checkable, but when the user tries to check it... then I will (possibly) prevent the checkbox from being checked and display a message.

like image 362
ycomp Avatar asked Feb 22 '12 15:02

ycomp


People also ask

How do you disable a checkbox if it is checked?

Using attr() function attr('disabled'); This function also takes a parameter to specify the property or the task that the selected element or checkbox is applied to. Whereas this function specifies the property for the checkbox object and it is specified as “disabled” for disabling the checkbox object.

How avoid checkbox checked or unchecked?

In this article, you are going to learn how you can prevent a checkbox from being unchecked using JavaScript preventDefault() method without disableing it. Use the preventDefault() method to stop the default behavior of a particular element. Learn more about preventDefault() method.

How do I stop a checkbox from being unchecked Android?

Just add the android:clickable="false" attribute in the layout xml. and it works fine.

How do you stop a checkbox being checked in angular 8?

preventDefault() to stop checkbox from been toggled. You can event add some logic before $event. preventDefault() to determin whether the checkbox should be prevent from changing status.


2 Answers

Just add the android:clickable="false" attribute in the layout xml.

So for me I have:

    <CheckBox         android:id="@+id/server_is_online"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:checked="true"         android:clickable="false"         android:text="@string/server_is_online"         android:textSize="23sp" /> 

and it works fine.

No that's probably not how you're supposed to use a checkbox, but I am using this as a dirty hack in the prototyping stage in lieu of having a nice icon with a green tick for all good, and an evil red cross for end of the world :)

like image 162
bobjandal Avatar answered Sep 19 '22 17:09

bobjandal


you can do something like this:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){      @Override     public void onCheckedChanged(CompoundButton buttonView,     boolean isChecked) {     if(isChecked){         cb.setChecked(false);         // Code to display your message.         }     } }); 
like image 36
Woodsy Avatar answered Sep 21 '22 17:09

Woodsy