Android辅助功能:如何更改大声朗读EditText视图的文本 [英] Android Acccessibility: How do I change the text read out loud for an EditText View

查看:231
本文介绍了Android辅助功能:如何更改大声朗读EditText视图的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Accessibility Services将为EditText视图读取以下内容

By default, the Accessibility Services will read out the following for an EditText view

  • 如果EditText输入了一个值=它将读出该值
  • 如果没有输入值=它将读出提示"

我希望它能读出两种情况下完全不同的东西.

I want it to read out something completely different in both cases.

我的xml代码段是

<EditText
    android:id="@+id/my_edit_text"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:editable="false"
    android:focusable="true"
    android:hint="my hint text"/>

我必须支持API 14及更高版本.

I have to support API 14 and onwards.

在这种情况下,我不想麻烦扩展EditText,因此我使用的是AccessibilityDelegate.

I do not want to go to the trouble of extending EditText for this one single case, therefore I am using am AccessibilityDelegate.

mEditTextView.setAccessibilityDelegate(accessibilityDelegate);

从文档中我了解到,在我的委托中,我只需要覆盖要更改其行为的委托中的那些方法.所有其他方法将默认为View的实现.

From the documentation I understand that in my delegate I only have to overwrite those methods in the delegate for which I would like to change the behaviour. All other methods will default to the View's implementation.

http://developer.android.com/reference/android/view/View.AccessibilityDelegate.html http://developer.android.com/reference/android/view/View.html

"onPopulateAccessibilityEvent"的文档说:"给主机View一个机会,以其文本内容填充可访问性事件." "dispatchPopulateAccessibilityEvent"的文档说:"先将AccessibilityEvent调度到主机View,然后再调度给其子级,以将其文本内容添加到事件中.",并且默认行为是调用"onPopulateAccessibilityEvent"查看视图本身,然后在其所有子视图上添加"dispatchPopulateAccessibilityEvent"

The doc for "onPopulateAccessibilityEvent" says : "Gives a chance to the host View to populate the accessibility event with its text content." The doc for "dispatchPopulateAccessibilityEvent" says : "Dispatches an AccessibilityEvent to the host View first and then to its children for adding their text content to the event." and that the default behaviour is to call "onPopulateAccessibilityEvent" for the view itself and then "dispatchPopulateAccessibilityEvent" on all its children

http://developer.android.com/guide/topics/ui/accessibility/apps.html

此文档在"onPopulateAccessibilityEvent"下说:"*如果此事件的实现完全覆盖输出文本而又不允许布局的其他部分修改其内容,则不要在代码中调用此方法的超级实现."

This doc says under "onPopulateAccessibilityEvent" "*If your implementation of this event completely overrides the output text without allowing other parts of your layout to modify its content, then do not call the super implementation of this method in your code."

因此,我的代表是以下

View.AccessibilityDelegate accessibilityDelegate = new View.AccessibilityDelegate() {
    @Override
    public void onPopulateAccessibilityEvent(View v, AccessibilityEvent event) {
        event.getText().add("Apples");
    }

};

为什么当我使用键盘导航或使用屏幕点击EditText视图时,它仍在读取我的提示文字"而不是"Apples"?

Why when I use the keyboard to navigate to or use the screen to tap on the EditText view it is still reading "my hint text" and not "Apples"?

如果我使用调试器,则会看到在设置事件文本之前,该文本为空,而在设置事件文本之后,该文本为"Apples",但TalkBack仍会读出提示.

If I use a debugger, I see that before I set the event text, the text is empty and after I set it, it is "Apples" yet the TalkBack still reads out the hint.

奇怪的是,如果我覆盖"onInitializeAccessibilityNodeInfo"并发送带有所需文本的事件,则该所需文本将被读出(请参见下面的代码片段).但这对我来说似乎是错误的,因为"onInitializeAccessibilityNodeInfo"正在对EditText的事件做出反应,但是随后又引发了一个新事件.

Weirdly if I overwrite "onInitializeAccessibilityNodeInfo" and send an event with my desired text, then this desired text gets read out (see code snippet below). But this seems wrong to me as the "onInitializeAccessibilityNodeInfo" is reacting to the EditText's event but then just raising a new one.

@Override  
public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfo info){  
    super.onInitializeAccessibilityNodeInfo(v, info);      
    ...   
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);  
    event.getText().add("Pears");  
    event.setClassName(className);  
    event.setPackageName(packageName);  
    ...     
    v.getParent().requestSendAccessibilityEvent(v, event);  
}

推荐答案

我们可以通过执行以下操作来更改从EditText视图大声读出的文本:

We can change the text read out loud of EditText view by doing the following:

View.AccessibilityDelegate accessibilityDelegate = new View.AccessibilityDelegate() {
    @Override
    public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(v, info);
        info.setText("some customized text")
    }  
};

,然后将此委托设置为EditText视图.

and then set this delegate to the EditText View.

这篇关于Android辅助功能:如何更改大声朗读EditText视图的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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