Android:如何强制通过触摸式浏览/对讲重复 AccessibilityFocus 中当前视图的 ContentDescription? [英] Android: How to force Explore-By-Touch/Talkback to repeat the ContentDescription of the current View in AccessibilityFocus?

查看:32
本文介绍了Android:如何强制通过触摸式浏览/对讲重复 AccessibilityFocus 中当前视图的 ContentDescription?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用中,我有一个 View,它显示一个图像并有一个关联的 ContentDescription.

In my Android app, I have a View which displays an image and has an associated ContentDescription.

我可以启用 Settings->Accessibility->Talkback,使用 Explore By Touch 从 Talkback 设置,然后单击视图以阅读 内容描述大声.

I can enable Settings->Accessibility->Talkback, with Explore By Touch from the Talkback settings, and then single tap on the View to read the ContentDescription aloud.

到目前为止,这一切都符合预期.

This is all as expected so far.

但是,如果我再次单击相同的 View,我希望 Explore By Touch 重复 ContentDescription.例如,我的图像和描述可能会在具有 AccessibilityFocus 时更新,因此说新短语对用户会有所帮助.我怎样才能做到这一点?

However, I would like Explore By Touch to repeat the ContentDescription if I single tap the same View again. For example, my image and description may be updated while it has the AccessibilityFocus, so saying the new phrase would be helpful to the user. How can I achieve this?

我尝试过使用 View.clearFocus()View.invalidate(),但没有成功.

I've tried experimenting with View.clearFocus() and View.invalidate(), but with no success.

欢迎提出任何建设性的想法.

Any constructive ideas would be welcome.

更新

我还应该提到我想从 API 8 开始添加支持.因此,我正在尝试使用 API 4 的辅助功能接口尽我所能.

I should have also mentioned that I want to add support from API 8 onwards. Hence, I'm trying to do the best I can with the Accessibility interface from API 4.

我目前正在研究使用 View.clearFocus() 后跟 View.requestFocus() 来解决更新视图问题的解决方案.到目前为止,这似乎适用于我使用 API 8 和 16 的测试套件.

I'm currently working on a solution that uses View.clearFocus() followed by View.requestFocus() to work around the updated-view issue. This appears to work so far on my test kit with APIs 8 and 16.

我会再次更新最终结果,但欢迎在此期间提供进一步的建议.

I'll update again with the final result, but welcome further suggestions in the interim.

推荐答案

正如我在问题中所承诺的,这里是我的调查结果和可行的解决方案.

As promised in my question, here are the results of my investigation and a working solution.

首先,非常感谢 alanv 的帮助和支持,以及为以后的 API(14 及以上)回答问题.尽管他没有找到旧 API 的解决方案,但他确实提供了帮助我找到答案的关键信息.

Firstly, many thanks to alanv for his help and support, and for answering the question for later APIs (14 and upwards). Although he didn't find the solution for older APIs, he did provide critical information which helped me find an answer.

在撰写本文时,由于 TalkBack 中的已知错误,无法构建成功说出的 AccessibilityEvent - 请参阅 发送未链接到视图的辅助功能事件以获取更多详细信息.如果事件源为空,则 TalkBack (v3.5.0_r105) 会忽略该事件,并且 AccessibilityEvent.setSource(View) 首次出现在 API 14,在此之前无法设置源 - 请参阅Google Eyes-Free 问题 #379

At the time of writing, it is not possible to construct a successfully-spoken AccessibilityEvent, due to a known bug in TalkBack - see Send accessibility event not linked to view for further details. If the event source is null, the event is ignored by TalkBack (v3.5.0_r105) and as AccessibilityEvent.setSource(View) first appeared in API 14, it is not possible to set the source prior to that - see Google Eyes-Free issue #379

但是,正如我之前暗示的那样,可以通过使用 View.requestFocus() 使 Android 生成此类事件.

However, it is possible to make Android generate such events by using View.requestFocus() as I previously hinted.

  1. 使您的视图在其 XML 布局中可聚焦 使用:

android:focusable="true"
android:focusableInTouchMode="true"

Activity.onCreate 中使用 View.setContentDescription(text) 或使用 android:contentDescription="text" 设置语音文本> 在 XML 布局中(如果它是静态的),或在代码中(如果需要)动态.

Set the spoken text using View.setContentDescription(text) in Activity.onCreate or with android:contentDescription="text" in the XML layout if it is static, or dynamically in code if necessary.

当需要语音文本时,使用 View.requestFocus() 将焦点移动到正确的视图以触发 AccessibilityEvent.

When the spoken text is required, move the focus to the correct view using View.requestFocus() to trigger an AccessibilityEvent.

为确保重复某个短语(我的原始问题),请在上一步请求焦点之前调用 View.clearFocus().

To ensure that a phrase is repeated (my original question), call View.clearFocus() before requesting focus in the previous step.

我有一个在 API 8 上工作的这种方法的测试实现,它也适用于 API 16.它应该一直工作到 API 4,当 Accessibility API 首次被引入.

I have a tested implementation of this approach working on API 8, and it also works on API 16. It should work all the way back to API 4, when the Accessibility API was first introduced.

仅支持较新的 Android API(14 及更高版本)的开发人员可能不需要此解决方案,但我相信对于那些支持较早 API 中的辅助功能的开发人员,目前几乎没有其他选择.祝你好运.

This solution may not be required for developers who only support more recent Android APIs (14 and later), but I believe there are few other options currently for those supporting Accessibility in earlier APIs. Good luck.

更新

不幸的是,alanv 的回答似乎已经消失了——可能被删除了——带走了评论中的所有相关细节和讨论.我试图重述下面的要点.

Unfortunately, alanv's answer seems to have disappeared - possibly deleted - taking with it all the associated detail and discussion in the comments. I have attempted to recapture the main points below.

  1. 对于 Android API 14 及更高版本,可以按如下方式创建焦点事件:

  1. For Android API 14 and later, it is possible to create focused events as follows:

if (mAccessibilityService.isEnabled()) {AccessibilityEvent 事件 = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_FOCUSED);event.setSource(myView);//API 14 &之后event.setClassName(myView.getClass().getName());event.setPackageName(myView.getContext().getPackageName());event.setEnabled(true);event.setContentDescription(文本);myView.sendAccessibilityEventUnchecked(event);}

对于 Android API 16 及更高版本,可以使用 View.announceForAccessibility(text).

For Android API 16 and later, View.announceForAccessibility(text) can be used.

这篇关于Android:如何强制通过触摸式浏览/对讲重复 AccessibilityFocus 中当前视图的 ContentDescription?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆