CheckBox: block the impact of the setChecked Method on OnCheckedChangeListener

Source: Internet
Author: User

CheckBox: block the impact of the setChecked Method on OnCheckedChangeListener

The OnCheckedChangeListener of the CheckBox can be triggered in two cases:
(1) The user clicked CheckBox;
(2) The setChecked (boolean checked) method is called in the code, and the checked parameter is inconsistent with the original selected status of the CheckBox.
Sometimes, you only need to listen to the (1) event and ignore the (2) event.

In fact, this requirement is very common. For example, there is a list in the page, the list contains multiple checkboxes for users to choose from, and the page has a fully-selected checkBox.

(A) When you select all the checkboxes, You must select all the checkboxes in the list;

(B) When you cancel the selection of all checkboxes, You Need To deselect all the checkboxes in the list;

(C) if the current status is all selected, the Select All button is deselected as long as the user unselects a checkBox in the list.

At this time, the problem occurs: When executing (c), you need to select all checkboxes. setChecked (false), which calls the OnCheckedChangeListener of the Select All button. Due to (B), all the checkboxes in the list will be deselected, which is not the expected result.

There are two ways to solve this problem:

Solution (1 ):

The program defines a global variable boolean ignoreChange, and then changes the value of ignoreChange when the setChecked method is called:

ignoreChange = true;selectAllCB.setChecked(true);ignoreChange = false;

Then, the ignoreChange is judged in the OnCheckedChangeListener of selectAllCB:

selectAllCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if (!ignoreChange) {                    //do something                }            }        });
Solution (2 ):

For checkBox, OnCheckedChangeListener is not registered, but OnClickListener is directly registered,

checkBox.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                boolean checked = ((CheckBox) v).isChecked();                Toast.makeText(CheckBoxTest.this, "isChecked?" + checked, Toast.LENGTH_SHORT).show();            }        });
The problem is solved perfectly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.