如何在Android上为可点击的视图修改“话语提示"用法提示? [英] How can I modify the TalkBack usage hint for a clickable View on Android?
问题描述
默认情况下,在内容描述之后,Android上可点击的视图将呈现为带有大声读取的用法提示(如果启用了话语提示,并且用户将焦点放在该视图上):
By default, clickable Views on Android will be rendered with a usage hint that's read aloud (if TalkBack is enabled and the user focuses on that View) after the content description:
双击激活"
我是否可以更改此设置,以便它读出的内容不那么抽象,而更特定于我的应用程序?喜欢:
Can I change this so it reads out something less abstract and more specific to my app? Like:
双击播放视频"
"Double tap to play video"
推荐答案
是的,这绝对有可能!
如果您具有自定义视图,则可以覆盖 onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
方法并添加具有 ACTION_CLICK
ID的操作,以覆盖标签:>
If you have a custom View, you can override the onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
method and add an action with the ACTION_CLICK
ID, to override the label:
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.addAction(
new AccessibilityNodeInfo.AccessibilityAction(
AccessibilityNodeInfo.ACTION_CLICK,
"play video"
)
);
}
如果该视图具有单击侦听器,则通过添加此新的 Action
,您将覆盖默认标签,因此TalkBack会改为说双击".
If that View has a click listener, then by adding this new Action
, you'll have overridden the default label so TalkBack will say "Double tap to " instead.
仅在API 21上可用-如果您想要在较低版本的API上运行的产品,或者想在非自定义视图上设置自定义使用提示,该怎么办?您可以使用 ViewCompat
和 AccessibilityDelegateCompat
!
This is only available on API 21 - what if you wanted something that worked on a lower API version or wanted to set a custom usage hint on a non-custom View? You can use ViewCompat
and AccessibilityDelegateCompat
!
非常相似-您可以在扩展的自定义AccessibilityDelegate中覆盖等效方法:
It's very similar - you can override the equivalent method in a custom AccessibilityDelegate that you extend:
public static class PlayVideoAccessibilityDelegate extends AccessibilityDelegateCompat {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.addAction(
new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_CLICK,
"play video"
)
);
}
}
然后使用它,您可以使用 ViewCompat
设置委托:
then to use it, you set the delegate with ViewCompat
:
ViewCompat.setAccessibilityDelegate(playButton, new PlayVideoAccessibilityDelegate());
使用辅助工具
Novoda有一个实用程序库,可帮助在Android上进行辅助功能访问.其中包括一些有助于设置使用提示的工具:
Using accessibilitools
Novoda has a utility library to help with accessibility on Android. This includes some tools to help set usage hints:
UsageHintsAccessibilityDelegate delegate = new UsageHintsAccessibilityDelegate(resources);
delegate.setClickLabel("play video");
ViewCompat.setAccesibilityDelegate(playButton, delegate);
我写了一个 blogpost,它是accessibilitools的概述(我也是库).
I wrote a blogpost which is an overview of accessibilitools (I am also a contributor to the library).
这篇关于如何在Android上为可点击的视图修改“话语提示"用法提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!