Modify accessibility focus order

前端 未结 1 1859
無奈伤痛
無奈伤痛 2021-02-07 03:03

Is it possible to change the accessibility focus order? For example, if I have 3 views side by side, with ids view1, view2, and view3, is there a simple way to make the accessi

相关标签:
1条回答
  • 2021-02-07 03:34

    For applications with minSdkVersion >= 22, you may set the traversal order for screen readers direct in the XML with android:accessibilityTraversalAfter:

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_launcher" />
    
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="30dp"
        android:accessibilityTraversalAfter="@id/imageView3"
        android:layout_height="30dp"
        android:src="@drawable/ic_launcher" />
    
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_launcher" />
    

    For applications supporting lower API levels, the traversal order may be set programmatically with ViewCompat:

    ViewCompat.setAccessibilityDelegate(imageView2, object : AccessibilityDelegateCompat() {
        override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfoCompat?) {
            info?.setTraversalAfter(imageView3)
            super.onInitializeAccessibilityNodeInfo(host, info)
        }
    })
    

    Please keep in mind that screen reader users rely on the consistency of the navigation, therefore, changing the focus order is an anti-pattern and should be avoided.

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