简体   繁体   English

如何使用Android辅助功能切换选中状态?

[英]How to toggle checked state using android accessibility service?

I am not able to toggle the checked state of ui node with class android.widget.Switch I have tried .performAction(AccessibilityNodeInfo.ACTION_SELECT) and .performAction(AccessibilityNodeInfo.ACTION_CLICK) but it didn't workout. 我无法使用类android.widget.Switch切换ui节点的选中状态,我尝试了.performAction(AccessibilityNodeInfo.ACTION_SELECT).performAction(AccessibilityNodeInfo.ACTION_CLICK)但是没有锻炼。 Thanks for the help.. 谢谢您的帮助..

You may set the state of the Switch as checked through its View object when some event occours (like a button click): 当发生某些事件(例如单击按钮)时,可以通过其View对象将Switch的状态设置为选中:

ViewCompat.setAccessibilityLiveRegion(mySwitch, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE)
mySwitch.isChecked = true

Setting the accessibility state as checked would be possible using AccessibilityDelegate , but for a Switch , the current View state (on or off) is more important than the one set from the delegate. 使用AccessibilityDelegate可以将可访问性状态设置为选中状态,但是对于Switch ,当前的View状态(打开或关闭)比委托中设置的状态更为重要。 Below is the code to set a custom AccessibilityDelegate to a View . 下面是将自定义AccessibilityDelegate设置为View的代码。

ViewCompat.setAccessibilityDelegate(mySwitch, object : AccessibilityDelegateCompat() {
    override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfoCompat?) {
        super.onInitializeAccessibilityNodeInfo(host, info)

        info?.isChecked = true
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM