10.1 ease of access
Many Android users have a disability and need to provide them with different ways to interact with Android devices.
Android provides an accessibility layer to help users more easily browse Android devices. This is also a detail aspect that makes your software more powerful. Let's take a good look.
10.1.1 allow navigation with a targeted Controller
Many Android devices have some targeted controllers, such:
- A trackball that a user can move in any direction.
- A d-pad that allows users to navigate in four directions.
- An arrow-like button and an OK button are equivalent to clicking a trackball or a D-pad.
By operating these controllers, you can navigate to other interfaces using different touch screens. It is recommended that you introduce the focus concept to control navigation without using touch screens.
Android provides several APIs to control whether a widget has a focus or even require a widget to have a focus. These methods include:
setFocusable()
isFocusable()
requestFocus()
This is the method in the Code. If you are using the XML layout file, you can useandroid:focusable
True"
10.1.1.1 control focus Sequence
When you want to use targeted control, the focus is transmitted from one view to another, which is determined by the focus sequence. The focus operation sequence is based on a rule that finds the nearest neighbor in a specific direction. Sometimes you may not want to use the default focus Transfer sequence. You want to define it yourself. Next let's take a look at the methods in XML in focus Order (the following methods are not described in detail if they are literal)
-
android:nextFocusDown
android:nextFocusLeft
-
android:nextFocusRight
-
android:nextFocusUp
For example, there is an XML layout file, and textview contains a focus. When textview is on the right side of edittext, you can press down arrow to transfer the focus to 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>
Of course, we can also modify the focus sequence at runtime, for example, using the View class'ssetNextFocusDownId()
And setnextfocusrightid ()
.
10.1.1.2 click with a targeted Controller
In most devices, clicking a view using a targeted controller will send a keyevent with the keycodeKEYCODE_DPAD_CENTER
To the view with the current focus. Make sure the event works the same when you touch the view on the touch screen.
Result. All standard Android views are processed as appropriate in advanceKEYCODE_DPAD_CENTER
. If possible, we recommend that youKEYCODE_ENTER
And keycode_dpad_center
As consistent.
10.1.2 set a label in your input part
Many input widgets have a prompt to let the user know what the input is. For example, a notepad app may use an image with "+" to prompt users to click it to add a new record. Or an edittext is near
View to indicate its purpose. Of course, this kind of prompt is useless for users with vision problems. To provide text prompts for such input parts, you can useandroid:contentDescription
Attribute. Or use barrier
Speech tool. Read the description of this attribute to many views.android:contentDescription
You can check this attribute when using it. The following is an example of imagebutton's use of this attribute.
<ImageButton
android:id=”@+id/add_entry_button”
android:src=”@drawable/plus”
android:contentDescription=”Add note”/>
10.1.3 send accessibility events from custom components
If you need to create a custom view component for your application, you may need to do some additional work to ensure that your view is accessible. Specifically, make sure that your view is implementedAccessibilityEventSource
Interface and
Send at appropriate timeAccessibilityevent, because eachAccessibilityEvent
Contains important information about the view status.
On the user interface, an event is sent when an important event occurs. Currently, there are five access events. view should send the event to the system for interaction with users:
-
TYPE_VIEW_CLICKED
-
Indicates that the user clicks a view (for example, the user selects a button)
-
TYPE_VIEW_LONG_CLICKED
-
Indicates that the user is pressing a view
-
TYPE_VIEW_SELECTED
-
Indicates that the user selects an item in the view. Usually applies to
AdapterView
Medium
-
TYPE_VIEW_FOCUSED
-
Indicates that the user moves the focus to view
-
TYPE_VIEW_TEXT_CHANGED
-
Indicates that the view text or content has been changed.
The basic view class is implementedAccessibilityEventSource
Send these events as appropriate. Your custom view should inherit the View class in Android.
Based on the features of our custom view, we can usesendAccessibilityEvent()
To send specific events
For example, if you define a slider, you can select a value left or right. When the slider value changes, a type_view_text_changed event is generated:
@Override
public boolean onKeyUp (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
mCurrentValue--;
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
return true;
}
...
}
Each accessibilityevent has a set of required attributes to describe the current status view. These attributes include the class name, text, and selected status of the view. The specific attribute descriptions required for each event type are in the accessibilityevent file.
View also has a dispatchpopulateaccessibilityevent () method. It is used to change the accessibilityevent object before the accessibilityevent is sent.
The slider above is used as an example. view should add the event Text of the current value of the slider:
@Override
public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event) {
super.dispatchPopulateAccessibilityEvent(event);
if (!isShown()) {
return false;
}
CharSequence text = String.valueOf(mCurrentValue);
if (text.length() > AccessibilityEvent.MAX_TEXT_LENGTH) {
text = text.subSequence(0, AccessiblityEvent.MAX_TEXT_LENGTH);
}
event.getText().add(text);
return true;
}