Android-以编程方式更改开关的状态而无需触发OnCheckChanged侦听器 [英] Android - programmatically change the state of a switch without triggering OnCheckChanged listener

查看:172
本文介绍了Android-以编程方式更改开关的状态而无需触发OnCheckChanged侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在寻找一种方法,该方法使用 switch.setChecked(true); 以编程方式更改Android Switch小部件的状态,而不会触发 OnCheckedChangedlistener .

我的第一个念头是将其换成 OnClickListener ,但是由于这只会记录点击次数,您不仅可以点击并滑动Switch,所以它真的不适合此用途就像用户将Switch从关闭滑动到on一样,那么Switch实际上不会做任何事情,因为用户没有单击...

如果有人对此有解决方案或明智的解决方法,那将是很棒


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

推荐答案

好吧,在使用开关执行代码操作之前,您可以先取消注册监听器,然后执行所需的操作,然后再次注册监听器.

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.

这篇关于Android-以编程方式更改开关的状态而无需触发OnCheckChanged侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆