Change Checkbox value without triggering onCheckChanged

前端 未结 19 982
广开言路
广开言路 2020-11-30 00:18

I have setOnCheckedChangeListener implemented for my checkbox

Is there a way I can call

checkbox.setChecked(false);
         


        
相关标签:
19条回答
  • 2020-11-30 00:48

    Try this one should work for you! You can use this with firebase also!

    For get firebase data! Use this!

    databaseReference.child(user.getPhoneNumber()).child("Reqs").addValueEventListener(new ValueEventListener() {
    
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                SharedPreferences prefs = mContext.getSharedPreferences("uinfo", MODE_PRIVATE);
                String pno = prefs.getString("username", "No name defined");
    
                if(dataSnapshot.child(pno).getValue(String.class).equals("acc")){
                    holder.acc.setChecked(true);
                }else{
                    holder.acc.setChecked(false);
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.w("dfs", "loadPost:onCancelled", databaseError.toException());
                // ...
            }
        });
    

    After that when user do something!

    holder.acc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked) {
                        if(buttonView.isPressed()) {
                            //your code
                        }
                    }
                    else {
                        if(buttonView.isPressed()) {
                           //your code
                        }
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-30 00:50

    My interpretation which i think is the easiest
    May be helpful)

    public class ProgrammableSwitchCompat extends SwitchCompat {
    
        public boolean isCheckedProgrammatically = false;
    
        public ProgrammableSwitchCompat(final Context context) {
            super(context);
        }
    
        public ProgrammableSwitchCompat(final Context context, final AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ProgrammableSwitchCompat(final Context context, final AttributeSet attrs, final int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void setChecked(boolean checked) {
            isCheckedProgrammatically = false;
            super.setChecked(checked);
        }
    
        public void setCheckedProgrammatically(boolean checked) {
            isCheckedProgrammatically = true;
            super.setChecked(checked);
        }
    }
    

    use it

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean on) {
        if (((ProgrammableSwitchCompat) compoundButton).isCheckedProgrammatically) {
            return;
        }
        //...
        ((ProgrammableSwitchCompat) compoundButton).setCheckedProgrammatically(true);
        //...
        ((ProgrammableSwitchCompat) compoundButton).setCheckedProgrammatically(false);
        //...
    }
    

    use will trigger setChecked(boolean) function
    that is all

    KOTLIN

    class MyCheckBox @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = R.attr.switchStyle
    ) : AppCompatCheckBox(context, attrs, defStyleAttr) {
    
        var programmatically = false
    
        override fun setChecked(checked: Boolean) {
            programmatically = false
            super.setChecked(checked)
        }
    
        fun setCheckedProgrammatically(checked: Boolean) {
            programmatically = true
            super.setChecked(checked)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 00:53

    Set null to changeListener before check radio button. You can set listener again after check radio button.

    radioGroup.setOnCheckedChangeListener(null);
    radioGroup.check(R.id.radioButton);
    radioGroup.setOnCheckedChangeListener(new 
    
    RadioGroup.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
    
       }
    });
    
    0 讨论(0)
  • 2020-11-30 00:54

    I found all the above answers way too complicated. Why not just create your own flag with a simple boolean?

    Just use a simple flag system with a boolean. Create boolean noListener. Whenever you want to turn your switch on/off without running any code (in this example, represented as runListenerCode(), simply set noListener=true before calling switch.setChecked(false/true)

    switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton compoundButton, boolean selected) {
                   if (!noListener) { //If we want to run our code like usual
                       runListenerCode();
                   } else { //If we simply want the switch to turn off
                       noListener = false;
                   }
               });
    

    Very simple solution using simple flags. At the end, we set noListener=false once again so that our code continues to work. Hope this helps!

    0 讨论(0)
  • 2020-11-30 00:54

    Is very simple, you just check isPressed inside setOnCheckedChangeListener

    Kotlin

    switch.setOnCheckedChangeListener { buttonView, isChecked ->
        when {        
            buttonView.isPressed -> {
                foo(isChecked)
            }
        }
    
    0 讨论(0)
  • 2020-11-30 00:57

    Add this code inside OnCheckedChangeListener:

    if(!compoundButton.isPressed()) {
                return;
    }
    

    This will help us to figure out weather checkBox state was changed programmatically or by user action.

    0 讨论(0)
提交回复
热议问题