Android - programmatically change the state of a switch without triggering OnCheckChanged listener

☆樱花仙子☆ 提交于 2021-02-17 15:06:55

问题



I'm looking for a method of programmatically changing the state of an Android Switch widget using switch.setChecked(true); without triggering OnCheckedChangedlistener.

My first thought was to swap it out for an OnClickListener but as this only registers clicks and you are able to not only click but also slide a Switch then it's not really fit for purpose as if the user was to slide the Switch from off to on then the Switch would actually do nothing as the user is not clicking...

If anyone's got a solution or a smart work around for this, that would be awesome


回答1:


Well, just before doing things in code with the switch you could just unregister the Listener, then do whatever you need to, and again register the listener.




回答2:


Set the listener to null before calling setCheck() function, and enable it after that, such as the following:

switch.setOnCheckedChangeListener (null);
switch.setChecked(true);
switch.setOnCheckedChangeListener (this);

Reference: Change Checkbox value without triggering onCheckChanged




回答3:


Every CompoundButton (two states button - on/off) has a pressed state which is true only when a user is pressing the view.

Just add a check in your listener before starting the actual logic:

if(compoundButton.isPressed()) {
    // continue with your listener
}

That way, changing the checked value programmatically won't trigger the unwanted code.

From @krisDrOid answer.




回答4:


I have one solution and its working fine at my end. I have added setOnTouchListener and setOnCheckedChangeListener on my switch control, and added following code to solve my problem.

    // set tag by default.
    mMySwitch.setTag("TAG");

    // Add OnCheckedChangeListener.
    mMySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mMySwitch.getTag() != null) {
                 mMySwitch.setTag(null);
                 return;
                }

          // Do your stuff here.
        }
    });

    // Add Touch listener.
    mMySwitch.setOnTouchListener(new View.OnTouchListener() {
         @Override
         public boolean onTouch(View v, MotionEvent event) {
            mMySwitch.setTag(null);
            return false;
         }
    });

In this way setOnCheckedChangeListener is getting called only when check changed happens by human intervention by drag, by click, by touch.

Also don't forgot to add your valid string tag ( not null ) when your trying to change check status of switch control. like :

 mMySwitch.setTag("TAG");
 mMySwitch.setChecked(true);



回答5:


Write a custom Switch or SwitchCompat and override the setOnCheckedListener.

public class SwitchCompat extends android.support.v7.widget.SwitchCompat {
private boolean mIgnoreCheckedChange = false;

public SwitchCompat(Context context) {
    super(context);
}

public SwitchCompat(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


@Override
public void setOnCheckedChangeListener(final OnCheckedChangeListener listener) {
    super.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mIgnoreCheckedChange) {
                return;
            }
            listener.onCheckedChanged(buttonView, isChecked);
        }
    });
}

public void setChecked(boolean checked, boolean notify) {
    mIgnoreCheckedChange = !notify;
    setChecked(checked);
    mIgnoreCheckedChange = false;
}

}



回答6:


use this switch.setChecked(true); // first set value (from pref or server or anywhere) and than switch.setOnCheckedChangeListener (this); add listener.




回答7:


Elaborating on Mahmoud's answer

CompoundButton.OnCheckedChangeListener switchListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    switchListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //Implement on check change
            }
        }; 
      switch.setOnCheckedChangeListener (switchListener);

      //When you want to trigger the checked of switch do the following
      switch.setOnCheckedChangeListener (null);
      switch.setChecked(true);
      switch.setOnCheckedChangeListener (switchListener);
     }

`



来源:https://stackoverflow.com/questions/25642052/android-programmatically-change-the-state-of-a-switch-without-triggering-onche

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