Android 上的協助工具Accessibility on Android

此頁面說明如何根據協助工具檢查清單,使用 Android 協助工具 api 來建立應用程式。This page describes how to use the Android Accessibility APIs to build apps according to the accessibility checklist. 如需其他平臺 Api,請參閱iOS 協助工具OS X 輔助功能頁面。Refer to the iOS accessibility and OS X accessibility pages for other platform APIs.

描述 UI 元素Describing UI Elements

Android 提供一個 ContentDescription 屬性,可供螢幕讀取 Api 用來提供控制項用途的可存取描述。Android provides a ContentDescription property that is used by screen reading APIs to provide an accessible description of the control's purpose.

內容描述可以在 AXML 配置檔案中C#的或中設定。The content description can be set in either C# or in the AXML layout file.

C#C#

可以在程式碼中將描述設定為任何字串(或字串資源):The description can be set in code to any string (or a string resource):

saveButton.ContentDescription = "Save data";

AXML 版面配置AXML layout

在 XML 版面配置中,使用 android:contentDescription 屬性:In XML layouts use the android:contentDescription attribute:

<ImageButton
    android:id=@+id/saveButton"
    android:src="@drawable/save_image"
    android:contentDescription="Save data" />

TextView 的 Use 提示Use Hint for TextView

針對資料輸入的 EditTextTextView 控制項,請使用 Hint 屬性來提供預期的輸入(而不是 ContentDescription)的描述。For EditText and TextView controls for data input, use the Hint property to provide a description of what input is expected (instead of ContentDescription). 輸入部分文字時,文字本身將會是 "read",而不是提示。When some text has been entered, the text itself will be "read" instead of the hint.

C#C#

在程式碼中設定 Hint 屬性:Set the Hint property in code:

someText.Hint = "Enter some text"; // displays (and is "read") when control is empty

AXML 版面配置AXML layout

在 XML 版面配置檔案中,使用 android:hint 屬性:In XML layout files use the android:hint attribute:

<EditText
    android:id="@+id/someText"
    android:hint="Enter some text" />

若要將標籤與資料輸入控制項建立關聯,請使用 LabelFor 屬性來To associate a label with a data input control, use the LabelFor property to

C#C#

在C#中,將 [LabelFor] 屬性設為此內容所描述之控制項的資源識別碼(通常會在標籤上設定此屬性,並參考一些其他輸入控制項):In C#, set the LabelFor property to the resource ID of the control that this content describes (typically this property is set on a label and references some other input control):

EditText edit = FindViewById<EditText> (Resource.Id.editFirstName);
TextView tv = FindViewById<TextView> (Resource.Id.labelFirstName);
tv.LabelFor = Resource.Id.editFirstName;

AXML 版面配置AXML layout

在版面配置 XML 中,使用 android:labelFor 屬性來參考另一個控制項的識別碼:In layout XML use the android:labelFor property to reference another control's identifier:

<TextView
    android:id="@+id/labelFirstName"
    android:hint="Enter some text"
    android:labelFor="@+id/editFirstName" />
<EditText
    android:id="@+id/editFirstName"
    android:hint="Enter some text" />

公告以取得協助工具Announce for Accessibility

當啟用協助工具時,在任何 view 控制項上使用 AnnounceForAccessibility 方法,將事件或狀態變更傳達給使用者。Use the AnnounceForAccessibility method on any view control to communicate an event or status change to users when accessibility is enabled. 大部分的作業都不需要這個方法,因為內建旁白會提供足夠的意見反應,但應使用於其他資訊對使用者很有説明的地方。This method isn't required for most operations where the built-in narration provides sufficient feedback, but should be used where additional information would be helpful for the user.

下列程式碼顯示呼叫 AnnounceForAccessibility 的簡單範例:The code below shows a simple example calling AnnounceForAccessibility:

button.Click += delegate {
  button.Text = string.Format ("{0} clicks!", count++);
  button.AnnounceForAccessibility (button.Text);
};

變更焦點設定Changing Focus Settings

可存取的導覽依賴具有焦點的控制項,以協助使用者瞭解有哪些作業可供使用。Accessible navigation relies on controls having focus to aid the user in understanding what operations are available. Android 提供 Focusable 屬性,可將控制項標記為在導覽期間特別能夠接收焦點。Android provides a Focusable property which can tag controls as specifically able to receive focus during navigation.

C#C#

若要防止控制項取得焦點C#,請將 Focusable 屬性設定為 falseTo prevent a control from gaining focus with C#, set the Focusable property to false:

label.Focusable = false;

AXML 版面配置AXML layout

在 [版面配置 XML 檔案] 中,設定 android:focusable 屬性:In layout XML files set the android:focusable attribute:

<android:focusable="false" />

您也可以使用 nextFocusDownnextFocusLeftnextFocusRight nextFocusUp 屬性來控制焦點順序,通常是在版面配置 AXML 中設定。You can also control focus order with the nextFocusDown, nextFocusLeft, nextFocusRight, nextFocusUp attributes, typically set in the layout AXML. 使用這些屬性可確保使用者可以輕鬆地流覽螢幕上的控制項。Use these attributes to ensure the user can navigate easily through the controls on the screen.

協助工具和當地語系化Accessibility and Localization

在上述範例中,[提示] 和 [內容] 描述會直接設定為顯示值。In the examples above the hint and content description are set directly to the display value. 最好是使用字串 .xml檔案中的值,如下所示:It is preferable to use values in a Strings.xml file, such as this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="enter_info">Enter some text</string>
    <string name="save_info">Save data</string>
</resources>

使用字串檔案中C#的文字,如下所示和 AXML 配置檔案:Using text from a strings file is shown below in C# and AXML layout files:

C#C#

不使用程式碼中的字串常值,而是從字串檔案中,使用 Resources.GetText 來查詢已翻譯的值:Instead of using string literals in code, look up translated values from strings files with Resources.GetText:

someText.Hint = Resources.GetText (Resource.String.enter_info);
saveButton.ContentDescription = Resources.GetText (Resource.String.save_info);

AXMLAXML

在版面配置 XML 可存取性屬性(如 hintcontentDescription)可以設定為字串識別碼:In layout XML accessibility attributes like hint and contentDescription can be set to a string identifier:

<TextView
    android:id="@+id/someText"
    android:hint="@string/enter_info" />
<ImageButton
    android:id=@+id/saveButton"
    android:src="@drawable/save_image"
    android:contentDescription="@string/save_info" />

將文字儲存在不同檔案中的優點是可以在您的應用程式中提供檔案的多種語言翻譯。The benefit of storing text in a separate file is multiple language translations of the file can be provided in your app. 請參閱Android 當地語系化指南,以瞭解如何將當地語系化的字串檔案加入至應用程式專案。See the Android localization guide to learn how add localized string files to an application project.

測試協助工具Testing Accessibility

請遵循下列步驟來啟用 TalkBack,並透過觸控來測試 Android 裝置上的協助工具。Follow these steps to enable TalkBack and Explore by Touch to test accessibility on Android devices.

You may need to install TalkBack from Google Play if it does not appear in Settings > Accessibility.You may need to install TalkBack from Google Play if it does not appear in Settings > Accessibility.