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

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

问题描述

在我的Android应用中,我有一个视图,该视图显示图像并具有关联的 ContentDescription

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

我可以启用 Settings-> Accessibility-> Talkback ,并在Talkback设置中使用 Explore By Touch ,然后单点击视图以大声读出 ContentDescription

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.

但是,如果我一次点击,我希望触摸浏览重复 ContentDescription code>查看。例如,当我的图像和说明具有 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中的Accessibility接口来做到最好。

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.

在撰写本文时,不可能由于话语提示中的一个已知错误,请构造一个成功语音的 AccessibilityEvent -请参见发送未链接到视图的可访问性事件以获取更多详细信息。如果事件源为空,则该事件将被TalkBack(v3.5.0_r105)忽略,因为 AccessibilityEvent.setSource(View)首先出现在 API 14 ,则无法设置在此之前的来源-请参见 Google免费版第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()就像我之前暗示的那样。

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

设置语音文字u在 Activity.onCreate 中或 android:contentDescription =中唱歌 View.setContentDescription(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 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.

仅支持较新版本Androi的开发人员可能不需要此解决方案d 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 event = 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(text);
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天全站免登陆