如何更改 Android 双击和长按的对讲指令
How to change Android talkback instructions for double tap and long press
我有一个具有长按操作处理程序的视图。我使用内容描述来设置当视图获得焦点时 Talkback 说话的消息。
目前它在获得焦点后立即显示我的内容描述,并在短暂的停顿后显示:
Double tap to activate, double tap and hold for long press
我想将此消息更改为类似
的内容
Double tap to "action 1", double tap and hold for "action 2"
有办法吗?
我查看了 onPopulateAccessibilityEvent()
,它收到了 TYPE_VIEW_ACCESSIBILITY_FOCUSED
事件,但我无法更改所需的消息。
我是不是漏掉了一些简单的东西?
在 API 21+ 中,您可以通过在视图 AccessibilityNodeInfo
上设置自定义操作来自定义操作名称。有两种方法:1) 设置 AccessibilityDelegate
并覆盖 onInitializeAccessibilityNodeInfo
委托方法或 2) 扩展视图的 class 并覆盖 onInitializeAccessibilityNodeInfo
.
无论哪种方式,您都将构建一个新的 AccessibilityAction and setting it on the node using AccessibilityNodeInfo.addAction。
如果您选择使用委托,您将为点击操作设置自定义描述,如下所示:
view.setAccessibilityDelegate(new AccessibilityDelegate() {
@Override
public void onInitializeAccessibilityNodeInfo(
View v, AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(v, info);
// A custom action description. For example, you could use "pause"
// to have TalkBack speak "double-tap to pause."
CharSequence description = getResources().getText(R.string.my_click_desc);
AccessibilityAction customClick = new AccessibilityAction(
AccessibilityAction.ACTION_CLICK, description);
info.addAction(customClick);
}
});
如果您的应用程序目标 API < 21,请替换为适当的 *Compat
支持库方法。该功能未向后移植,因此您不会在 API < 21 上获得自定义描述,但您将能够避免在应用程序代码中进行显式版本检查。
似乎 AccessibilityAction
自从 alanv 发布了他的回答后发生了轻微的变化。这是一个使用 ViewCompat
:
的工作解决方案
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
// A custom action description. For example, you could use "pause"
// to have TalkBack speak "double-tap to pause."
CharSequence description = host.getResources().getText(R.string.my_click_desc);
AccessibilityActionCompat customClick = new AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_CLICK, description);
info.addAction(customClick);
}
});
对于那些想要删除 all 短语 ie 的人,请使用下面的代码。 "double tap to".
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS);
}
});
这基本上是在调用以下代码,requestFocus
没有任何与之关联的默认对讲通知。
case AccessibilityNodeInfo.ACTION_FOCUS: {
if (!hasFocus()) {
// Get out of touch mode since accessibility
// wants to move focus around.
getViewRootImpl().ensureTouchMode(false);
return requestFocus();
}
}
对于那些想要删除 all 短语 ie 的人,请使用下面的代码。 "double tap to activate", "double tap and hold for long press".
mSubTitleText = (TextView) view.findViewById(R.id.txt_subtitle);
ViewCompat.setAccessibilityDelegate(mSubTitleText, new AccessibilityDelegateCompat() {
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
host.setClickable(false);
host.setLongClickable(false);
}
});
这是一个示例:
ViewCompat.setAccessibilityDelegate(set_actions_button, object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(v: View, info: AccessibilityNodeInfoCompat) {
super.onInitializeAccessibilityNodeInfo(v, info)
info.addAction(AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_CLICK, "Edit note"))
info.addAction(AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_LONG_CLICK, "Copy note"))
}
})
我有一个具有长按操作处理程序的视图。我使用内容描述来设置当视图获得焦点时 Talkback 说话的消息。
目前它在获得焦点后立即显示我的内容描述,并在短暂的停顿后显示:
Double tap to activate, double tap and hold for long press
我想将此消息更改为类似
的内容Double tap to "action 1", double tap and hold for "action 2"
有办法吗?
我查看了 onPopulateAccessibilityEvent()
,它收到了 TYPE_VIEW_ACCESSIBILITY_FOCUSED
事件,但我无法更改所需的消息。
我是不是漏掉了一些简单的东西?
在 API 21+ 中,您可以通过在视图 AccessibilityNodeInfo
上设置自定义操作来自定义操作名称。有两种方法:1) 设置 AccessibilityDelegate
并覆盖 onInitializeAccessibilityNodeInfo
委托方法或 2) 扩展视图的 class 并覆盖 onInitializeAccessibilityNodeInfo
.
无论哪种方式,您都将构建一个新的 AccessibilityAction and setting it on the node using AccessibilityNodeInfo.addAction。
如果您选择使用委托,您将为点击操作设置自定义描述,如下所示:
view.setAccessibilityDelegate(new AccessibilityDelegate() {
@Override
public void onInitializeAccessibilityNodeInfo(
View v, AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(v, info);
// A custom action description. For example, you could use "pause"
// to have TalkBack speak "double-tap to pause."
CharSequence description = getResources().getText(R.string.my_click_desc);
AccessibilityAction customClick = new AccessibilityAction(
AccessibilityAction.ACTION_CLICK, description);
info.addAction(customClick);
}
});
如果您的应用程序目标 API < 21,请替换为适当的 *Compat
支持库方法。该功能未向后移植,因此您不会在 API < 21 上获得自定义描述,但您将能够避免在应用程序代码中进行显式版本检查。
似乎 AccessibilityAction
自从 alanv 发布了他的回答后发生了轻微的变化。这是一个使用 ViewCompat
:
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
// A custom action description. For example, you could use "pause"
// to have TalkBack speak "double-tap to pause."
CharSequence description = host.getResources().getText(R.string.my_click_desc);
AccessibilityActionCompat customClick = new AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_CLICK, description);
info.addAction(customClick);
}
});
对于那些想要删除 all 短语 ie 的人,请使用下面的代码。 "double tap to".
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS);
}
});
这基本上是在调用以下代码,requestFocus
没有任何与之关联的默认对讲通知。
case AccessibilityNodeInfo.ACTION_FOCUS: {
if (!hasFocus()) {
// Get out of touch mode since accessibility
// wants to move focus around.
getViewRootImpl().ensureTouchMode(false);
return requestFocus();
}
}
对于那些想要删除 all 短语 ie 的人,请使用下面的代码。 "double tap to activate", "double tap and hold for long press".
mSubTitleText = (TextView) view.findViewById(R.id.txt_subtitle);
ViewCompat.setAccessibilityDelegate(mSubTitleText, new AccessibilityDelegateCompat() {
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
host.setClickable(false);
host.setLongClickable(false);
}
});
这是一个示例:
ViewCompat.setAccessibilityDelegate(set_actions_button, object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(v: View, info: AccessibilityNodeInfoCompat) {
super.onInitializeAccessibilityNodeInfo(v, info)
info.addAction(AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_CLICK, "Edit note"))
info.addAction(AccessibilityActionCompat(
AccessibilityNodeInfoCompat.ACTION_LONG_CLICK, "Copy note"))
}
})