`

【翻译】(23-补丁2)使应用程序无障碍

 
阅读更多

【翻译】(23-补丁2)使应用程序无障碍

 

see

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

 

原文见

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

 

-------------------------------

 

Making Applications Accessible

 

使应用程序无障碍

 

-------------------------------

 

In this document

 

本文目录

 

* Labeling User Interface Elements 标签用户界面元素

* Enabling Focus Navigation 使能焦点导航

* Enabling view focus 使能视图焦点

* Controlling focus order 控制焦点顺序

* Building Accessible Custom Views 构建无障碍的自定义视图

* Handling directional controller clicks 处理直接的控制器点击

* Implementing accessibility API methods 实现无障碍API方法

* Sending accessibility events 发送无障碍事件

* Populating accessibility events 填充无障碍事件

* Testing Accessibility 测试无障碍

* Testing audible feedback 测试听觉反馈

* Testing focus navigation 测试焦点导航

 

Key classes

 

关键类

 

AccessibilityEvent

AccessibilityNodeInfo

AccessibilityNodeInfoCompat

View.AccessibilityDelegate

AccessibilityDelegateCompat

 

See also

 

另见

 

Implementing Accessibility 实现无障碍

Designing Effective Navigation 设计有效的导航

Android Design Android设计

 

-------------------------------

 

Applications built for Android are accessible to users with visual, physical or age-related disabilities when they activate accessibility features and services on a device. By default, these services make your application more accessible. However, there are further steps you should take to optimize the accessibility of your application and ensure a pleasant experience for all your users.

 

为Android而构建的应用程序对于有视觉上、身体上、或年龄相关的障碍的用户来说是无障碍的,当他们激活设备上的无障碍特性和服务时,默认,这些服务使你的应用程序更加无障碍。然而,你应该执行一些更进一步的步骤来优化你的应用程序的无障碍并且确保对于你的所有用户有一个愉快的体验。

 

Making sure your application is accessible to all users is relatively easy, particularly when you use framework-provided user interface components. If you only use these standard components for your application, there are just a few steps required to ensure your application is accessible:

 

确保你的应用程序对于所有用户是无障碍的,是比较容易的,尤其当你使用框架提供的用户界面组件。如果你为你的应用程序只使用这些标准组件,只有一些必需的步骤来确保你的应用程序是无障碍的:

 

1. Label your ImageButton, ImageView, EditText, CheckBox and other user interface controls using the android:contentDescription attribute.

 

1. 使用android:contentDescription属性来标签你的ImageButton,ImageView,EditText,CheckBox和其它用户界面控件。

 

2. Make all of your user interface elements accessible with a directional controller, such as a trackball or D-pad.

 

2. 使你的所有用户界面元素对于一个方向控制器来说是可访问的,诸如一个轨迹球或方向板。

 

3. Test your application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls.

 

3. 通过打开无障碍服务像TalkBack(注:TalkBack是Android上的一个屏幕阅读器软件,见https://play.google.com/store/apps/details?id=com.google.android.marvin.talkback)和触碰探索(注:explore-by-touch是Android 4的新特性,见http://www.android.com/about/ice-cream-sandwich/,允许不看屏幕地导航)测试你的应用程序,并且尝试只使用方向控制来使用你的应用程序。

 

Developers who create custom controls that extend from the View class have some additional responsibilities for making sure their components are accessible for users. This document also discusses how to make custom view controls compatible with accessibility services.

 

创建扩展自View类的自定义组件的开发者有一些额外的责任确保他们的组件对于用户来说是无障碍的。本文档还讨论如何使自定义视图控件兼容无障碍服务。

 

-------------------------------

 

Labeling User Interface Elements

 

标签用户界面元素

 

Many user interface controls rely on visual cues to inform users of their meaning. For example, a note-taking application might use an ImageButton with a picture of a plus sign to indicate that the user can add a new note. Or, an EditText component may have a label near it that indicates its purpose. When a user with impaired vision accesses your application, these visual cues are often useless.

 

许多用户界面控件依赖于视觉暗示(注:视觉线索,视觉提示)来告诉用户它们的含义,例如,一个写便签的应用程序可能使用一个带有一个加号图片的ImageButton来指示用户可以添加一个新的便签。或者,一个EditText组件可能有一个标签在它旁边以指示它的用途。当一位视障用户访问你的应用程序时,这些视觉暗示常常是无用的。

 

To provide textual information about interface controls (as an alternative to the visual cues), use the android:contentDescription attribute. The text you provide in this attribute is not visible on the screen, but if a user has enabled accessibility services that provide audible prompts, then the description in this attribute is read aloud to the user.

 

为了提供关于界面控件的文本信息(作为视觉暗示的替换),请使用android:contentDescription属性。你在这个属性中提供的文本在屏幕上不是可见的,但如果一位用户已经使能一些提供听觉提示的无障碍服务,那么这个属性中的描述被大声地读给用户听。

 

Set the android:contentDescription attribute for every ImageButton, ImageView, EditText, CheckBox in your application's user interface, and on any other input controls that might require additional information for users who are not able to see it.

 

为你的应用程序的用户界面中每个ImageButton,ImageView,EditText,CheckBox,以及在对于不能看到它的用户可能需要额外信息的其它任意输入控件,设置android:contentDescription属性。

 

For example, the following ImageButton sets the content description for the plus button to the add_note string resource, which could be defined as “Add note" for an English language interface:

 

例如,以下ImageButton为加号按钮设置内容描述为add_note字符串资源,对于英语语言界面来说它可能被定义为“添加便签”。

 

-------------------------------

 

<ImageButton

    android:id=”@+id/add_note_button”

    android:src=”@drawable/add_note”

    android:contentDescription=”@string/add_note”/>

 

-------------------------------

 

By including the description, speech-based accessibility services can announce "Add note" when a user moves focus to this button or hovers over it.

 

通过包含描述,当用户移动焦点到这个按钮或悬浮在它上方时,基于语音的无障碍服务可以宣告“添加便签”。

 

-------------------------------

 

Note: For EditText fields, provide an android:hint attribute to help users understand what content is expected.

 

注意:对于EditText域,提供一个android:hint属性以帮助用户理解它期待什么内容。

 

-------------------------------

 

-------------------------------

 

Enabling Focus Navigation

 

使能焦点导航

 

Focus navigation allows users with disabilities to step through user interface controls using a directional controller. Directional controllers can be physical, such as a clickable trackball, directional pad (D-pad) or arrow keys, tab key navigation with an attached keyboard or a software application, such as the Eyes-Free Keyboard, that provides an on-screen directional control.

 

焦点导航允许有障碍的用户使用一个方向控制器遍历(注:步进)用户界面控件。方向控制器可以是物理的,诸如一个可点击轨迹球,方向板(D-pad)或方向键,使用一个外接(注:依附)键盘的tab键导航或一个软件应用程序,诸如Eyes-Free键盘(注:eyes-free是一个项目,见http://code.google.com/p/eyes-free/),提供一种屏幕上的方向控制。

 

A directional controller is a primary means of navigation for many users. Verify that all user interface (UI) controls in your application are accessible without using the touchscreen and that clicking with the center button (or OK button) of a directional controller has the same effect as touching the controls on the touchscreen. For information on testing directional controls, see Testing focus navigation.

 

一个方向控制器对于许多用户来说是一种主要的导航方式。验证在你的应用程序中所有用户界面(UI)控件在不使用触摸屏的情况下是无障碍的,并且用一个方向控制器的中间按钮(或OK按钮)点击拥有与触碰触摸屏上的控件拥有相同的效果。想获得关于测试方向控制的信息,请参见测试焦点导航。

 

Enabling view focus

 

使能视图焦点

 

A user interface element is accessible using directional controls when its android:focusable attribute is set to true. This setting allows users to focus on the element using the directional controls and then interact with it. The user interface controls provided by the Android framework are focusable by default and visually indicate focus by changing the control’s appearance.

 

一个用户界面元素使用方向控制是无障碍的,当它的android:focusable属性被设置为true。这个设置允许用户使用方向控制把焦点放在该元素上然后与它交互。Android框架提供的用户界面控件默认是可获得焦点的并且通过改变控件的外观可视地指示焦点。

 

Android provides several APIs that let you control whether a user interface control is focusable and even request that a control be given focus:

 

Android提供几个API让你控制一个用户界面控件是否可获得焦点,以及甚至请求一个空间被给予焦点:

 

* setFocusable()

* isFocusable()

* requestFocus()

 

When working with a view that is not focusable by default, you can make it focusable from the XML layout file by setting the android:focusable attribute to true or by using the setFocusable() method.

 

当处理一个默认不可获得焦点的视图时,你可以从XML布局中通过设置android:focusable属性为true或使用setFocusable()方法使它可获得焦点。

 

Controlling focus order

 

控制焦点顺序

 

When users navigate in any direction using directional controls, focus is passed from one user interface element (View) to another, as determined by the focus ordering. The ordering of the focus movement is based on an algorithm that finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the order that you intended for your UI. In these situations, you can provide explicit overrides to the ordering using the following XML attributes in the layout file:

 

当用户使用方向控制在任意方向导航时,焦点从一个用户界面元素(View)被传递至另一个,正如焦点顺序所决定的那样。焦点移动的排序是基于一种算法,它在一个给定的方向上查找最近邻居。在极少的情况下,默认算法可能不匹配你对你的用户界面想要的顺序。在这些情况下,你可以提供对使用布局文件中以下XML属性的排序的显式覆盖。

 

android:nextFocusDown

 

Defines the next view to receive focus when the user navigates down.

 

定义下一个视图以接收焦点,当用户导航向下时。

 

android:nextFocusLeft

 

Defines the next view to receive focus when the user navigates left.

 

定义下一个视图以接收焦点,当用户导航向左时。

 

android:nextFocusRight

 

Defines the next view to receive focus when the user navigates right.

 

定义下一个视图以接收焦点,当用户导航向右时。

 

android:nextFocusUp

 

Defines the next view to receive focus when the user navigates up.

 

定义下一个视图以接收焦点,当用户导航向上时。

 

The following example XML layout shows two focusable user interface elements where the android:nextFocusDown and android:nextFocusUp attributes have been explicitly set. The TextView is located to the right of the EditText. However, since these properties have been set, the TextView element can now be reached by pressing the down arrow when focus is on the EditText element:

 

以下示例XML布局展示两个可获得焦点的用户界面元素,在那里android:nextFocusDown和android:nextFocusUp属性已经被显示地设置。TextView被定位到EditText的右边。然而,自从这些属性被设置,TextView元素现在可以被到达,通过当焦点在EditText元素上时按向下箭头:

 

-------------------------------

 

<LinearLayout android:orientation="horizontal"

        ... >

    <EditText android:id="@+id/edit"

        android:nextFocusDown=”@+id/text”

        ... />

    <TextView android:id="@+id/text"

        android:focusable=”true”

        android:text="Hello, I am a focusable TextView"

        android:nextFocusUp=”@id/edit”

        ... />

</LinearLayout>

 

-------------------------------

 

When modifying focus order, be sure that the navigation works as expected in all directions from each user interface control and when navigating in reverse (to get back to where you came from).

 

当修改焦点顺序,请确保从每个用户界面控件中导航如期待那样在所有方向上工作,以及当反向导航(以回到你来自的地方)时。

 

-------------------------------

 

Note: You can modify the focus order of user interface components at runtime, using methods such as setNextFocusDownId() and setNextFocusRightId().

 

注意:你可以在运行时修改用户界面组件的焦点顺序,使用方法诸如setNextFocusDownId()和setNextFocusRightId()。

 

-------------------------------

 

Building Accessible Custom Views

 

构建无障碍的自定义视图

 

If your application requires a custom view component, you must do some additional work to ensure that your custom view is accessible. These are the main tasks for ensuring the accessibility of your view:

 

如果你的应用程序需要一个自定义视图组件,你必须做一些额外的工作以确保你的自定义视图是无障碍的。这些是主要任务用于确保你的视图的无障碍:

 

* Handle directional controller clicks

 

* 处理方向控制器点击

 

* Implement Accessibility API methods

 

* 实现无障碍API方法

 

* Send AccessibilityEvent objects specific to your custom view

 

* 发送AccessibilityEvent对象特定地给你的自定义视图

 

* Populate AccessibilityEvent and AccessibilityNodeInfo for your view

 

* 为你的视图填充AccessibilityEvent和AccessibilityNodeInfo

 

Handling directional controller clicks

 

处理方向控制器点击

 

On most devices, clicking a view using a directional controller sends a KeyEvent with KEYCODE_DPAD_CENTER to the view currently in focus. All standard Android views already handle KEYCODE_DPAD_CENTER appropriately. When building a custom View control, make sure this event has the same effect as touching the view on the touchscreen.

 

在大多数设备上,使用一个方向控制器点击一个视图会发送一个带KEYCODE_DPAD_CENTER的KeyEvent到当前获得焦点的视图。所有标准Android视图已经正确地处理KEYCODE_DPAD_CENTER。当构建一个自定义View控件时,请确保这个事件拥有和在触摸屏上触碰视图相同的效果。

 

Your custom control should also treat the KEYCODE_ENTER event the same as KEYCODE_DPAD_CENTER. This approach makes interaction from a full keyboard much easier for users.

 

你的自定义控件还应该与KEYCODE_DPAD_CENTER相同地对待KEYCODE_ENTER事件。这种方式使来自一个全键盘的交互对于用户来说更容易得多。

 

Implementing accessibility API methods

 

实现无障碍API方法

 

Accessibility events are messages about users interaction with visual interface components in your application. These messages are handled by Accessibility Services, which use the information in these events to produce supplemental feedback and prompts when users have enabled accessibility services. As of Android 4.0 (API Level 14) and higher, the methods for generating accessibility events have been expanded to provide more detailed information beyond the AccessibilityEventSource interface introduced in Android 1.6 (API Level 4). The expanded accessibility methods are part of the View class as well as the View.AccessibilityDelegate class. The methods are as follows:

 

无障碍事件是关于用户与你的应用程序中的可视化界面组件交互的消息。这些消息被无障碍服务处理,它使用这些消息中的信息来产生补充反馈和提示,当用户已经使能无障碍服务时。从Android 4.0(API级别14)和更高开始,产生无障碍事件的方法已经被扩展以提供在Android 1.6(API级别4)中引入的AccessibilityEventSource之上更详细的信息。被扩展的无障碍方法是View类和View.AccessibilityDelegate类的部分。这些方法如下:

 

sendAccessibilityEvent()

 

(API Level 4) This method is called when a user takes action on a view. The event is classified with a user action type such as TYPE_VIEW_CLICKED. You typically do not need to implement this method unless you are creating a custom view.

 

(API级别4)此方法被调用,当一个用户在一个视图上执行动作。事件用一个用户动作类型诸如TYPE_VIEW_CLICKED来分类。通常你不需要实现这个方法,除非你正在创建一个自定义视图。

 

sendAccessibilityEventUnchecked()

 

(API Level 4) This method is used when the calling code needs to directly control the check for accessibility being enabled on the device (AccessibilityManager.isEnabled()). If you do implement this method, you must assume that the calling method has already checked that accessibility is enabled and the result is true. You typically do not need to implement this method for a custom view.

 

(API级别4)此方法被使用,当调用方代码需要直接地控制设备上无障碍被使能(AccessibilityManager.isEnabled())的检查。如果你实现此方法,你必须假设调用方方法已经检查无障碍被使能并且结果是true。通常对于一个自定义视图你不需要实现此方法。

 

dispatchPopulateAccessibilityEvent()

 

(API Level 4) The system calls this method when your custom view generates an accessibility event. As of API Level 14, the default implementation of this method calls onPopulateAccessibilityEvent() for this view and then the dispatchPopulateAccessibilityEvent() method for each child of this view. In order to support accessibility services on revisions of Android prior to 4.0 (API Level 14) you must override this method and populate getText() with descriptive text for your custom view.

 

(API级别4)系统调用此方法,当你的自定义视图生成一个无障碍事件。从API级别14开始,此方法的默认实现调用这个视图的onPopulateAccessibilityEvent(),然后是这个视图的每个子视图的dispatchPopulateAccessibilityEvent()。为了在4.0(API级别14)之前的Android修订版上提供无障碍服务,你必须覆盖这个方法并且用你的自定义视图的描述文本(注:描述你的自定义视图的文本)来填充getText()。

 

onPopulateAccessibilityEvent()

 

(API Level 14) This method sets the text output of an AccessibilityEvent for your view. This method is also called if the view is a child of a view which generates an accessibility event.

 

(API级别14)这个方法设置你的视图的一个AccessibilityEvent的文本输出。这个方法也被调用,如果视图是一个生成无障碍事件的视图的子视图。

 

-------------------------------

 

Note: Modifying additional attributes beyond the text within this method potentially overwrites properties set by other methods. So, while you are able modify attributes of the accessibility event with this method, you should limit these changes to text content only and use the onInitializeAccessibilityEvent() method to modify other properties of the event.

 

注意:在这个方法中修改文本上的额外属性会隐式地覆写被其它方法设置的属性。所以,当你有能力用这个方法修改无障碍事件的属性,你应该只限制对文本内容的这些改变并且使用onInitializeAccessibilityEvent()方法来修改事件的其它属性。

 

-------------------------------

 

-------------------------------

 

Note: If your implementation of this event calls for completely overiding 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.

 

注意:如果你对此事件的实现要求完整地覆盖(注:无视)输出文本而不允许你的布局的其它部分修改它的内容,那么不要在你的代码中调用这个方法的超类实现。

 

-------------------------------

 

onInitializeAccessibilityEvent()

 

(API Level 14) The system calls this method to obtain additional information about the state of the view, beyond text content. If your custom view provides interactive control beyond a simple TextView or Button, you should override this method and set the additional information about your view into the event using this method, such as password field type, checkbox type or states that provide user interaction or feedback. If you do override this method, you must call its super implementation and then only modify properties that have not been set by the super class.

 

(API级别14)系统调用此方法来取出关于视图状态的额外信息,它在文本内容之上(注:它超越文本内容)。如果你的自定义视图提供一个简单TextView或Button上的交互控制,你应该覆盖这个方法来使用此方法把关于你的视图的附加信息设置进该事件,诸如密码域类型,勾选框类型或提供用户交互的状态或反馈。如果你覆盖这个方法,你必须调用这个超类实现,然后只修改不曾被超类设置的属性。

 

onInitializeAccessibilityNodeInfo()

 

(API Level 14) This method provides accessibility services with information about the state of the view. The default View implementation sets a standard set of view properties, but if your custom view provides interactive control beyond a simple TextView or Button, you should override this method and set the additional information about your view into the AccessibilityNodeInfo object handled by this method.

 

(API级别14)这个方法用关于视图状态的信息提供无障碍服务。默认View实现设置一个标准的视图属性集合,但如果你的自定义视图提供一个简单TextView或Button上的交互控制,你应该覆盖这个方法并把关于你的视图的额外信息设置进被这个方法处理的AccessibilityNodeInfo对象。

 

onRequestSendAccessibilityEvent()

 

(API Level 14) The system calls this method when a child of your view has generated an AccessibilityEvent. This step allows the the parent view to amend the accessibility event with additional information. You should implement this method only if your custom view can have child views and if the parent view can provide context information to the accessibility event that would be useful to accessibility services.

 

(API级别14)系统调用此方法,当你的视图的一个子视图已经生成一个AccessibilityEvent。这个步骤允许父视图用额外的信息修正无障碍事件。你应该实现这个方法,只有如果你的自定义视图可以有子视图,而且如果父视图可以提供上下文信息给将对于无障碍服务有用的无障碍事件。

 

In order to support these accessibility methods for a custom view, you should take one of the following approaches:

 

为了让一个自定义视图支持这些无障碍方法,你应该执行以下其中一种方法:

 

* If your application targets Android 4.0 (API level 14) and higher, override and implement the accessibility methods listed above directly in your custom view class.

 

* 如果你的应用程序把目标定为Android 4.0(API级别14)和更高,请直接地在你的自定义视图类中覆盖和实现上面列举的无障碍方法。

 

* If your custom view is intended to be compatible with Android 1.6 (API Level 4) and above, add the Android Support Library, revision 5 or higher, to your project. Then, within your custom view class, call the ViewCompat.setAccessibilityDelegate() method to implement the accessibility methods above. For an example of this approach, see the Android Support Library (revision 5 or higher) sample AccessibilityDelegateSupportActivity in (<sdk>/extras/android/support/v4/samples/Support4Demos/)

 

* 如果你的自定义视图倾向于兼容Android 1.6(API级别4)和以上,请添加Android支持库修订版5或更高到你的工程。然后,在你的自定视图类中,调用ViewCompat.setAccessibilityDelegate()方法来实现上面的无障碍方法。想获得这种方法的一个示例,请参见<sdk>/extras/android/support/v4/samples/Support4Demos/(注:此处多了一对括号)中的Android支持库(修订版5或更高)示例AccessibilityDelegateSupportActivity。

 

In either case, you should implement the following accessibility methods for your custom view class:

 

在两种情况之一,你应该为你的自定义视图类实现以下无障碍方法:

 

* dispatchPopulateAccessibilityEvent()

* onPopulateAccessibilityEvent()

* onInitializeAccessibilityEvent()

* onInitializeAccessibilityNodeInfo()

 

For more information about implementing these methods, see Populating Accessibility Events.

 

想获得关于实现这些方法的更多信息,请参见填充无障碍事件。

 

Sending accessibility events

 

发送无障碍事件

 

Depending on the specifics of your custom view, it may need to send AccessibilityEvent objects at a different times or for events not handled by the default implementation. The View class provides a default implementation for these event types:

 

依赖于你的自定义视图的细节,可能需要在一个不同的时刻上(注:此处times貌似应为单数)或为不被默认实现处理的事件而发送AccessibilityEvent对象。View类提供为这些事件类型提供一个默认实现:

 

* Starting with API Level 4:

 

* 从API级别4开始:

 

* TYPE_VIEW_CLICKED

* TYPE_VIEW_LONG_CLICKED

* TYPE_VIEW_FOCUSED

 

* Starting with API Level 14:

 

* 从API级别14开始:

 

* TYPE_VIEW_SCROLLED

* TYPE_VIEW_HOVER_ENTER

* TYPE_VIEW_HOVER_EXIT

 

-------------------------------

 

Note: Hover events are associated with the Explore by Touch feature, which uses these events as triggers for providing audible prompts for user interface elements.

 

注意:悬浮事件与触碰探索(注:Explore by Touch)特性有关,它使用这些事件作为用于提供用户界面元素的听觉提示的触发器。

 

-------------------------------

 

In general, you should send an AccessibilityEvent whenever the content of your custom view changes. For example, if you are implementing a custom slider bar that allows a user to select a numeric value by pressing the left or right arrows, your custom view should emit an event of type TYPE_VIEW_TEXT_CHANGED whenever the slider value changes. The following sample code demonstrates the use of the sendAccessibilityEvent() method to report this event.

 

通常,你应该发送一个AccessibilityEvent,每当你的自定义视图的内容改变时。例如,如果你正在实现一个自定义滑动条,它允许一个用户通过按左或右箭头来选择一个数字值。你的自定义视图应该发送一个类型为TYPE_VIEW_TEXT_CHANGED的事件,每当滑动条的值改变时,以下示例代码演示如何使用sendAccessibilityEvent()方法来报告这个事件。

 

-------------------------------

 

@Override

public boolean onKeyUp (int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

        mCurrentValue--;

        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);

        return true;

    }

    ...

}

 

-------------------------------

 

Populating accessibility events

 

填充无障碍事件

 

Each AccessibilityEvent has a set of required properties that describe the current state of the view. These properties include things such as the view’s class name, content description and checked state. The specific properties required for each event type are described in the AccessibilityEvent reference documentation. The View implementation provides default values for these properties. Many of these values, including the class name and event timestamp, are provided automatically. If you are creating a custom view component, you must provide some information about the content and characteristics of the view. This information may be as simple as a button label, but may also include additional state information that you want to add to the event.

 

每个AccessibilityEvent拥有一组必需的属性,它们描述视图的当前状态。这些属性包括一些东西诸如视图的类名,内容描述和勾选状态。对于每个事件类型必需的特定属性被描述在AccessibilityEvent参考文档中。该View实现提供这些属性的默认值。这些值中许多,包括类名和事件时间戳,被自动地提供。如果你正在创建一个自定义视图组件,你必须提供关于该视图的内容和特性的一些信息。这些信息可能简单到一个按钮的标签,但还可能包括你想添加到事件的附加状态信息。

 

The minimum requirement for providing information to accessibility services with a custom view is to implement dispatchPopulateAccessibilityEvent(). This method is called by the system to request information for an AccessibilityEvent and makes your custom view compatible with accessibility services on Android 1.6 (API Level 4) and higher. The following example code demonstrates a basic implementation of this method.

 

用一个自定义视图提供信息给无障碍服务的最小需求是实现dispatchPopulateAccessibilityEvent()。这个方法被系统调用以请求一个AccessibilityEvent的信息并且使你的自定义视图兼容Android 1.6(API级别4)和更高上的无障碍服务。以下示例代码演示这个方法的一个基本实现。

 

-------------------------------

 

@Override

public void dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {

    super.dispatchPopulateAccessibilityEvent(event);

    // Call the super implementation to populate its text to the event, which

    // calls onPopulateAccessibilityEvent() on API Level 14 and up.

    // 调用超类实现以填充它的文本到事件,

    // 它在API级别14和更高上调用onPopulateAccessibilityEvent()。

 

    // In case this is running on a API revision earlier that 14, check

    // the text content of the event and add an appropriate text

    // description for this custom view:

    // 万一它正在运行在一个早于14的API修订版上,

    // 请检查事件的文本内容并且为这个自定义视图

    // 添加一个合适的文本描述:

    CharSequence text = getText();

    if (!TextUtils.isEmpty(text)) {

        event.getText().add(text);

    }

}

 

-------------------------------

 

On Android 4.0 (API Level 14) and higher, the onPopulateAccessibilityEvent() and onInitializeAccessibilityEvent() methods are the recommended way to populate or modify the information in an AccessibilityEvent. Use the onPopulateAccessibilityEvent() method specifically for adding or modifying the text content of the event, which is turned into audible prompts by accessibility services such as TalkBack. Use the onInitializeAccessibilityEvent() method for populating additional information about the event, such as the selection state of the view.

 

在Android 4.0(API级别14)和更高上,onPopulateAccessibilityEvent()和onInitializeAccessibilityEvent()方法是推荐的方式以填充或修改一个AccessibilityEvent里的信息。使用onPopulateAccessibilityEvent()方法特别地为了添加或修改该事件的文本上下文,它被转换为听觉提示,通过无障碍服务诸如TalkBack。使用onInitializeAccessibilityEvent()方法用于填充关于事件的额外信息,诸如视图的选择状态。

 

In addition, you should also implement the onInitializeAccessibilityNodeInfo() method. AccessibilityNodeInfo objects populated by this method are used by accessibility services to investigate the view hierarchy that generated an accessibility event after receiving that event, to obtain a more detailed context information and provide appropriate feedback to users.

 

另外,你还应该实现onInitializeAccessibilityNodeInfo()方法。被这个方法填充的AccessibilityNodeInfo对象被无障碍服务使用以调查在接收那个事件之后生成一个无障碍事件的视图层级,以取出一个更详细的上下文信息并提供合适的反馈给用户。

 

The example code below shows how override these three methods by using ViewCompat.setAccessibilityDelegate(). Note that this sample code requires that the Android Support Library for API Level 4 (revision 5 or higher) is added to your project.

 

以下示例展示如何通过使用ViewCompat.setAccessibilityDelegate()覆盖这三个方法。注意这个示例代码需要用于API级别4(修订版5或更高)的Android支持库被添加到你的工程。

 

-------------------------------

 

ViewCompat.setAccessibilityDelegate(new AccessibilityDelegateCompat() {

    @Override

    public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {

        super.onPopulateAccessibilityEvent(host, event);

        // We call the super implementation to populate its text for the

        // event. Then we add our text not present in a super class.

        // Very often you only need to add the text for the custom view.

        // 我们调用超类实现以为该事件生成它的文本。

        // 然后我们添加在超类中不存在的我们的文本。

        // 你常常只需要为自定义视图添加文本。

        CharSequence text = getText();

        if (!TextUtils.isEmpty(text)) {

            event.getText().add(text);

        }

    }

    @Override

    public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {

        super.onInitializeAccessibilityEvent(host, event);

        // We call the super implementation to let super classes

        // set appropriate event properties. Then we add the new property

        // (checked) which is not supported by a super class.

        // 我们调用超类实现以让超类

        // 设置合适的事件属性。然后我们添加不被超类支持的

        // 新属性(checked)

        event.setChecked(isChecked());

    }

    @Override

    public void onInitializeAccessibilityNodeInfo(View host,

            AccessibilityNodeInfoCompat info) {

        super.onInitializeAccessibilityNodeInfo(host, info);

        // We call the super implementation to let super classes set

        // appropriate info properties. Then we add our properties

        // (checkable and checked) which are not supported by a super class.

        // 我们调用超类实现以让超类设置

        // 合适的info属性。然后我们添加不被超类支持的

        // 我们的属性(checkable和checked)。

        info.setCheckable(true);

        info.setChecked(isChecked());

        // Quite often you only need to add the text for the custom view.

        // 你常常只需要为自定义视图添加文本。

        CharSequence text = getText();

        if (!TextUtils.isEmpty(text)) {

            info.setText(text);

        }

    }

}

 

-------------------------------

 

On applications targeting Android 4.0 (API Level 14) and higher, these methods can be implemented directly in your custom view class. For another example of this approach, see the Android Support Library (revision 5 or higher) sample AccessibilityDelegateSupportActivity in (<sdk>/extras/android/support/v4/samples/Support4Demos/).

 

在目标定为Android 4.0(API级别14)和更高的应用程序上,这些方法可以直接地被实现在你的自定义视图类中。想获得这种方法的另一个示例,请参见在<sdk>/extras/android/support/v4/samples/Support4Demos/(注:此处多了一对括号)中的Android支持库(修订版5或更高)示例AccessibilityDelegateSupportActivity。

 

-------------------------------

 

Note: You may find information on implementing accessibility for custom views written prior to Android 4.0 that describes the use of the dispatchPopulateAccessibilityEvent() method for populating AccessibilityEvents. As of the Android 4.0 release, however, the recommended approach is to use the onPopulateAccessibilityEvent() and onInitializeAccessibilityEvent() methods.

 

注意:你可以找到关于为在Android 4.0之前编写的自定义视图实现无障碍的信息,它描述用于填充AccessibilityEvent的dispatchPopulateAccessibilityEvent()方法的用法。然而,从Android 4.0发布开始,建议的方法是使用onPopulateAccessibilityEvent()和onInitializeAccessibilityEvent()方法。

 

-------------------------------

 

Testing Accessibility

 

测试无障碍

 

Testing the accessibility of your application is an important part of ensuring your users have a great experience. You can test the most important parts of accessibility by testing your application with audible feedback enabled and testing navigation within your application using directional controls.

 

测试你的应用程序的无障碍是一个重要的部分以确保你的用户拥有一个极佳的体验。你可以通过用被使能的听觉反馈来测试你的应用程序,以及在你的应用程序中使用方向控制来测试导航,以测试无障碍的一些最重要的部分。

 

Testing audible feedback

 

测试听觉反馈

 

You can simulate the experience for many users by enabling an accessibility service that speaks as you move around the screen. The Explore by Touch accessibility service, which is available on devices with Android 4.0 and later. The TalkBack accessibility service, by the Eyes-Free Project comes preinstalled on many Android devices.

 

你可以通过使能一个无障碍服务,它在你绕屏幕移动时说话,以模拟许多用户的体验。触碰探索(注:Explore by Touch)的无障碍服务,它在带Android 4.0和更新的设备上可用。(注:以及)TalkBack无障碍服务,通过在许多Android设备上开始被预装的Eyes-Free项目。

 

To enable TalkBack on revisions of Android prior to Android 4.0:

 

为了你在Android 4.0之前的Android修订版上使能TalkBack:

 

1. Launch the Settings application.

 

1. 启动设置应用程序

 

2. Navigate to the Accessibility category and select it.

 

2. 导航到无障碍分类并选择它。

 

3. Select Accessibility to enable it.

 

3. 选择无障碍以使能它。

 

4. Select TalkBack to enable it.

 

4. 选择TalkBack以使能它。

 

-------------------------------

 

Note: If the TalkBack accessibility service is not available, you can install it for free from Google Play.

 

注意:如果TalkBack无障碍服务不可用,你可以从Google Play中免费地安装它。

 

-------------------------------

 

To enable Explore by Touch on Android 4.0 and later:

 

为了在Android 4.0和更新上使能触碰探索:

 

1. Launch the Settings application.

 

1. 启动设置应用程序。

 

2. Navigate to the Accessibility category and select it.

 

2. 导航到无障碍分类并选择它。

 

3. Select the TalkBack to enable it.

 

3. 选择TalkBack以使能它。

 

4. Return to the Accessibility category and select Explore by Touch to enable it.

 

4. 返回到无障碍分类并选择触碰探索以使能它。

 

-------------------------------

 

Note: You must turn on TalkBack first, otherwise this option is not available.

 

注意:你必须首先打开TalkBack,否则这个选项是不可用的。

 

-------------------------------

 

Testing focus navigation

 

测试焦点导航

 

As part of your accessibility testing, you can test navigation of your application using focus, even if your test devices does not have a directional controller. The Android Emulator provides a simulated directional controller that you can easily use to test navigation. You can also use a software-based directional controller, such as the one provided by the Eyes-Free Keyboard to simulate use of a D-pad.

 

作为你的无障碍测试的一部分,你可以使用焦点测试你的应用程序的导航,即便你的测试设备没有一个方向控制器。Android模拟器提供一个模拟的方向控制器,你可以轻易地使用它来测试导航。你还可以使用一个基于软件的方向控制器,诸如Eyes-Free键盘提供的方向方向控制器以模拟方向板的使用。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 07 Apr 2012 1:06

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

此页部分内容,是基于Android开源项目所创建和共享的工作,并且根据知识共享2.5署名许可证描述的条款来使用的修改版。

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics