Android 上的辅助功能
本页介绍如何使用 Android 辅助功能 API 根据 辅助功能清单生成应用。 有关其他平台 API,请参阅 iOS 辅助功能和OS X 辅助功能 页面。
描述 UI 元素
Android 提供了一个 ContentDescription
属性,该属性由屏幕读取 API 用来提供控件用途的辅助说明。
可以在 C# 或 AXML 布局文件中设置内容说明。
C#
可以在代码中将说明设置为任何字符串 (或字符串资源) :
saveButton.ContentDescription = "Save data";
AXML 布局
在 XML 布局中, android:contentDescription
使用 属性:
<ImageButton
android:id=@+id/saveButton"
android:src="@drawable/save_image"
android:contentDescription="Save data" />
对 TextView 使用提示
对于 EditText
数据输入的 和 TextView
控件,请使用 Hint
属性提供预期输入 (的说明, ContentDescription
而不是) 。
输入某些文本后,文本本身将“读取”,而不是提示。
C#
在 Hint
代码中设置 属性:
someText.Hint = "Enter some text"; // displays (and is "read") when control is empty
AXML 布局
在 XML 布局文件中, android:hint
使用 属性:
<EditText
android:id="@+id/someText"
android:hint="Enter some text" />
LabelFor 将输入字段与标签链接
若要将标签与数据输入控件相关联,请使用 LabelFor
属性
C#
在 C# 中,将 LabelFor
属性设置为此内容描述的控件的资源 ID, (通常此属性是在标签上设置的,并引用其他一些输入控件) :
EditText edit = FindViewById<EditText> (Resource.Id.editFirstName);
TextView tv = FindViewById<TextView> (Resource.Id.labelFirstName);
tv.LabelFor = Resource.Id.editFirstName;
AXML 布局
在布局中, android:labelFor
XML 使用 属性引用另一个控件的标识符:
<TextView
android:id="@+id/labelFirstName"
android:hint="Enter some text"
android:labelFor="@+id/editFirstName" />
<EditText
android:id="@+id/editFirstName"
android:hint="Enter some text" />
辅助功能公告
AnnounceForAccessibility
启用辅助功能时,对任何视图控件使用 方法可将事件或状态更改传达给用户。 对于内置旁白提供足够反馈的大多数操作,此方法不是必需的,但应在其他信息对用户有帮助的情况下使用。
下面的代码显示了一个调用 AnnounceForAccessibility
的简单示例:
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
button.AnnounceForAccessibility (button.Text);
};
更改焦点设置
辅助导航依赖于具有焦点的控件来帮助用户了解哪些操作可用。 Android 提供了一个 Focusable
属性,该属性可将控件标记为在导航期间专门能够接收焦点。
C#
若要防止控件使用 C# 获得焦点,请将 Focusable
属性设置为 false
:
label.Focusable = false;
AXML 布局
在布局 XML 文件中, android:focusable
设置 属性:
<android:focusable="false" />
还可以使用 nextFocusDown
、、 nextFocusLeft
nextFocusRight
nextFocusUp
属性控制焦点顺序,这些属性通常在布局 AXML 中设置。 使用这些属性可确保用户可以轻松地在屏幕上的控件中导航。
辅助功能和本地化
在上面的示例中,提示和内容说明直接设置为显示值。 最好在 Strings.xml 文件中使用值,如下所示:
<?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 布局文件中如下所示:
C#
使用 从字符串文件中 Resources.GetText
查找已翻译的值,而不是在代码中使用字符串文本:
someText.Hint = Resources.GetText (Resource.String.enter_info);
saveButton.ContentDescription = Resources.GetText (Resource.String.save_info);
AXML
在布局中,XML 辅助功能属性(如 hint
和 contentDescription
)可以设置为字符串标识符:
<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" />
将文本存储在单独的文件中的好处是,可以在应用中提供文件的多种语言翻译。 请参阅 Android 本地化指南 ,了解如何将本地化字符串文件添加到应用程序项目。
测试辅助功能
按照以下步骤启用 TalkBack 和通过触摸浏览来测试 Android 设备上的辅助功能。
如果“设置辅助功能”>中未显示 TalkBack,则可能需要从 Google Play 安装 TalkBack。