TV 应用有时会有一些用例,使得依赖TalkBack的用户难以使用该应用。为了为这些用户提供更好的 TalkBack 体验,请查看本指南中的每个部分,并在必要时在您的应用中实施更改。如果您的应用使用自定义视图,您还应该参考相应的指南,其中介绍了如何使用自定义视图支持无障碍功能。
处理嵌套视图
嵌套视图对于 TalkBack 用户来说可能难以导航。在任何可能的情况下,使父视图或子视图中的一个可由 TalkBack 获取焦点,但不要同时使两者都可获取焦点。
要使视图可由 TalkBack 获取焦点,请将focusable
和focusableInTouchMode
属性设置为true
。此步骤是必要的,因为某些电视设备可能会在 TalkBack 处于活动状态时进入和退出触摸模式。
要使视图不可获取焦点,只需将focusable
属性设置为false
即可。但是,如果该视图是可操作的(即,其对应的AccessibilityNodeInfo
具有ACTION_CLICK
),它可能仍然可获取焦点。
更改操作说明
默认情况下,TalkBack 会为可操作视图宣布“按选择键激活”。对于某些操作,术语“激活”可能不是一个好的描述。为了提供更准确的描述,您可以更改它
Kotlin
findViewById<View>(R.id.custom_actionable_view).accessibilityDelegate = object : View.AccessibilityDelegate() { override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) { super.onInitializeAccessibilityNodeInfo(host, info) info.addAction( AccessibilityAction( AccessibilityAction.ACTION_CLICK.id, getString(R.string.custom_label) ) ) } }
Java
findViewById(R.id.custom_actionable_view).setAccessibilityDelegate(new AccessibilityDelegate() { @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(host, info); AccessibilityAction action = new AccessibilityAction( AccessibilityAction.ACTION_CLICK.getId(), getString(R.string.custom_label)); info.addAction(action); } });
添加对滑块的支持
TV 上的 TalkBack 对滑块有特殊支持。要启用滑块模式,请将ACTION_SET_PROGRESS
与RangeInfo
对象一起添加到视图中。
用户通过按下电视遥控器的中央按钮进入滑块模式。在此模式下,箭头按钮会生成ACTION_SCROLL_FORWARD
和ACTION_SCROLL_BACKWARD
无障碍操作。
以下示例实现了一个音量滑块,其级别从 1 到 10
Kotlin
/** * This example only provides slider functionality for TalkBack users. Additional logic should * be added for other users. Such logic may reuse the increase and decrease methods. */ class VolumeSlider(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) { private val min = 1 private val max = 10 private var current = 5 private var textView: TextView? = null init { isFocusable = true isFocusableInTouchMode = true val rangeInfo = AccessibilityNodeInfo.RangeInfo( AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_INT, min.toFloat(), max.toFloat(), current.toFloat() ) accessibilityDelegate = object : AccessibilityDelegate() { override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) { super.onInitializeAccessibilityNodeInfo(host, info) info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS) info.rangeInfo = rangeInfo } override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { if (action == AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD.id) { increase() return true } if (action == AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD.id) { decrease() return true } return super.performAccessibilityAction(host, action, args) } } } override fun onFinishInflate() { super.onFinishInflate() textView = findViewById(R.id.text) textView!!.text = context.getString(R.string.level, current) } private fun increase() { update((current + 1).coerceAtMost(max)) } private fun decrease() { update((current - 1).coerceAtLeast(min)) } private fun update(newLevel: Int) { if (textView == null) { return } val newText = context.getString(R.string.level, newLevel) // Update the user interface with the new volume. textView!!.text = newText // Announce the new volume. announceForAccessibility(newText) current = newLevel } }
Java
/** * This example only provides slider functionality for TalkBack users. Additional logic should * be added for other users. Such logic can reuse the increase and decrease methods. */ public class VolumeSlider extends LinearLayout { private final int min = 1; private final int max = 10; private int current = 5; private TextView textView; public VolumeSlider(Context context, AttributeSet attrs) { super(context, attrs); setFocusable(true); setFocusableInTouchMode(true); RangeInfo rangeInfo = new RangeInfo(RangeInfo.RANGE_TYPE_INT, min, max, current); setAccessibilityDelegate( new AccessibilityDelegate() { @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(host, info); info.addAction(AccessibilityAction.ACTION_SET_PROGRESS); info.setRangeInfo(rangeInfo); } @Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (action == AccessibilityAction.ACTION_SCROLL_FORWARD.getId()) { increase(); return true; } if (action == AccessibilityAction.ACTION_SCROLL_BACKWARD.getId()) { decrease(); return true; } return super.performAccessibilityAction(host, action, args); } }); } @Override protected void onFinishInflate() { super.onFinishInflate(); textView = findViewById(R.id.text); textView.setText(getContext().getString(R.string.level, current)); } private void increase() { update(Math.min(current + 1, max)); } private void decrease() { update(Math.max(current - 1, min)); } private void update(int newLevel) { if (textView == null) { return; } String newText = getContext().getString(R.string.level, newLevel); // Update the user interface with the new volume. textView.setText(newText); // Announce the new volume. announceForAccessibility(newText); current = newLevel; } }