Java Android - 以编程方式更改开关的状态而不触发 OnCheckChanged 侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25642052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - programmatically change the state of a switch without triggering OnCheckChanged listener
提问by Paul Alexander
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
我正在寻找一种使用switch.setChecked(true);
不触发OnCheckedChangedlistener
.
我的第一个想法是把它换成一个,OnClickListener
但因为这只会记录点击次数,你不仅可以点击,还可以滑动一个 Switch 那么它真的不适合目的,就好像用户要将 Switch 从关闭滑动到打开然后Switch 实际上不会做任何事情,因为用户没有点击......
如果有人有解决方案或聪明的解决方案,那就太棒了
采纳答案by smnpl
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.
好吧,就在使用 switch 在代码中执行操作之前,您可以取消注册侦听器,然后执行任何您需要的操作,然后再次注册侦听器。
回答by Chetan Bhoyar
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.
我有一个解决方案,它在我的最后工作正常。我在我的开关控件上添加了 setOnTouchListener 和 setOnCheckedChangeListener,并添加了以下代码来解决我的问题。
// 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.
这样 setOnCheckedChangeListener 只有在通过拖动、点击、触摸等人为干预发生检查更改时才会被调用。
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);
回答by Mahmoud Ibrahim
Set the listener to nullbefore calling setCheck()function, and enableit after that, such as the following:
在调用setCheck()函数之前将侦听器设置为null,然后再启用它,例如:
switch.setOnCheckedChangeListener (null);
switch.setChecked(true);
switch.setOnCheckedChangeListener (this);
Reference: Change Checkbox value without triggering onCheckChanged
回答by Ismail Iqbal
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);
}
`
`
回答by hyb1996
Write a custom Switch or SwitchCompat and override the setOnCheckedListener.
编写自定义 Switch 或 SwitchCompat 并覆盖 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;
}
}
回答by ronginat
Every CompoundButton(two states button - on/off) has a pressedstate which is true only when a user is pressing the view.
每个CompoundButton(两种状态按钮 - 开/关)都有一个按下状态,只有当用户按下视图时才为真。
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 @krisDrOidanswer.
来自@krisDrOid 的回答。