谷歌对讲API在Android中获取当前焦点项目



我的应用程序支持辅助功能,并且应用程序同时具有纵向和横向模式。

在我的屏幕中,我有一些视图,如按钮、文本视图、带有自定义行的列表视图。

我面临的问题是,当用户在纵向模式下聚焦任何项目并旋转屏幕时,应用程序在横向模式下不会聚焦相同的元素。有人可以建议如何将焦点设置为纵向模式下选择的项目到横向模式吗?

我什至对现有的应用程序进行了一些研究,例如本机设置应用程序-wifi页面和" ES文件探索",在这些应用程序中,当用户将方向更改为横向模式时,也不会保持可访问性。系统正在横向选择一些随机元素进行纵向拍摄,反之亦然。

下面是代码片段

accessibility_sample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Name"
android:focusableInTouchMode="true"
android:text="Name" />
<TextView
android:id="@+id/email_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Email"
android:focusableInTouchMode="true"
android:text="Email" />
<Button
android:id="@+id/sample_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Button description"
android:text="ButtonText" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Checkbox description"
android:text="Checkbox Text" />
<ListView
android:id="@+id/sample_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false" >
</ListView>
</LinearLayout>

sample_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
>
<CheckBox
android:id="@+id/list_row_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</CheckBox>
<TextView
android:id="@+id/list_row_name"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:focusable="true"
android:layout_height="wrap_content" />
</LinearLayout>

辅助功能示例活动.java

公共类 AccessibilitySampleActivity 扩展活动 {

private String TAG = AccessibilitySampleActivity.class.getSimpleName();
ListView sampleList;
Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accessibility_sample);
sampleList = (ListView) findViewById(R.id.sample_list);
myButton = (Button) findViewById(R.id.sample_button);
ArrayList<String> countriesList = new ArrayList<String>();
countriesList.add("India");
countriesList.add("America");
countriesList.add("China");
countriesList.add("Swis");
countriesList.add("Paries");
countriesList.add("Pak");
countriesList.add("Aus");
countriesList.add("Afg");
countriesList.add("Nedharnalds");
countriesList.add("Bangladhesh");
countriesList.add("Srilanka");
countriesList.add("France");
countriesList.add("Japan");
countriesList.add("SouthAfrica");
countriesList.add("Iran");
countriesList.add("Malaysia");
countriesList.add("Nepal");
sampleList.setAdapter(new CustomArrayAdapter(this, R.layout.sample_list_row, countriesList));
}
class CustomArrayAdapter extends ArrayAdapter<String> {
Context context;
int resource;
ArrayList<String> countriesList;
class ViewHolder {
TextView name;
}
public CustomArrayAdapter(Context context, int resource, ArrayList<String> countriesList) {
super(context, resource, countriesList);
this.context = context;
this.resource = resource;
this.countriesList = countriesList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(resource, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.list_row_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(getItem(position));
return convertView;
}
@Override
public int getCount() {
return super.getCount();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static View findAccessibilityFocus(View view) {
if (view == null)
return view;
if (view.isAccessibilityFocused())
return view;
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
View result = findAccessibilityFocus(childView);
if (result != null)
return result;
}
}
return null;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged");
AccessibilityManager am = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
boolean isAccessibilityEnabled = am.isEnabled();
boolean isExploreByTouchEnabled = am.isTouchExplorationEnabled();
Log.d(TAG, "isAccessibilityEnabled:" + isAccessibilityEnabled + " isExploreByTouchEnabled:"
+ isExploreByTouchEnabled);
if (isAccessibilityEnabled) {
View activityView = this.findViewById(android.R.id.content);
Log.d(TAG, "activityView:" + activityView);
View selectedView = findAccessibilityFocus(activityView);
if (selectedView != null) {
Log.d(TAG, "selectedView:" + selectedView);
selectedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
}
}

}

对于文本视图、按钮、复选框等普通视图,它能够在用户旋转屏幕时保持状态。

如果用户选择列表视图,以下是我面临的问题

  • 如果在纵向和旋转屏幕中选择了任何视图,则不会保持横向焦点。有时,如果所选视图能够在不滚动的情况下显示(例如,如果用户选择印度并旋转印度将在不滚动的情况下可见),它会保持状态
  • 当用户将屏幕从纵向旋转到横向时,应用滚动并选择任何视图(如日本)并将横向方向更改为纵向模式,然后我们可以看到它始终选中第一行复选框,即印度复选框

WCAG 目前没有指定动态(应用运行时)方向更改后焦点应该发生什么情况。 它确实讨论了方向变化,但没有讨论如何处理这种上下文变化,这是有充分理由的。焦点管理不是这里的核心问题!事实上,如果有的话,WCAG 会阻止您尝试使用解决方案,因为它可能会被视为干扰辅助技术的默认操作。 想一想,在你的问题中,你指出大多数应用程序"不做"你想做的事情。 如果你要完成这件事,而世界上所有其他应用程序都没有,那么谁是真正无法访问的?"预期行为"不是有益的吗?

其中无法访问的部分是令人惊讶的方向变化,而不是完全屏幕重新绘制后焦点发生的情况!在我看来,这样做的动机可能是对WCAG 2.0 3.2.#指南的错误解释。如果用户在关心焦点位置时意外导致方向更改......这确实是一个问题!你只是抓住了问题的错误部分。

焦点控制对于辅助功能非常重要,但方向更改与其他上下文更改不同。 在方向更改中,屏幕可能完全不同。 当然,屏幕上会有类似的元素,但不能保证聚焦的元素仍然存在。 与其尝试跟踪焦点并将其移动到适当的(现在可能不在屏幕元素),不如让 AT 做它的事情,并且对方向何时发生变化更具选择性。 最佳做法如下:

  • 切勿将用户锁定在特定方向。 请记住,用户可能将他们的设备安装在他们面前,因此要求方向是非常糟糕的。移动指南 4.1.
  • 但是,话虽如此,一旦确定用户(当使用触摸探索的辅助技术是其中之一)处于他们熟悉的方向时,请锁定方向,并为他们提供另一种机制来手动更改方向。 这样,用户可以自由地做一些事情,比如把手机放在耳朵上,而不必担心意外触发方向变化。WCAG 2.0 - 3.2.5
  • 如果方向发生更改,请允许默认行为。 即使在您看来无法访问它,只要用户没有意外触发方向更改,它可能不会那么令人沮丧。 如果您已正确解决上述问题,则不会成为问题。
  • 最后,请记住,当您开发辅助功能时,TalkBack 只是一种辅助技术,盲人只是该技术用户的一个子集。 虽然您可能正在改善盲人TalkBack用户的用户体验,但您必须考虑为使用对讲来支持其身体残疾的TalkBacks用户,盲文用户或SwitchAccess用户所做的工作。 通常,支持多个 AT 意味着允许操作系统、AT 和用户代理尽可能频繁地执行默认/支持的操作。 虽然添加黑客解决方案可能会改善盲人TalkBack用户的体验,但在TalkBack v#.#上,它很容易使当前SwitchAccess用户的情况变得更糟,甚至会破坏TalkBack v#.#+1用户。 这种黑客解决方案不好的概念实际上也在WCAG 4.1下包含在WCAG中。

总之,从可访问性的角度来看,您尝试做的事情确实有些误导,并且实际上会使事情变得更糟。 话虽如此,在 Android 视图中跟踪辅助功能焦点的概念很有趣,我将在下面为您留下代码。 但是,在这种情况下,我建议不要使用这种方法。

原文:

这很简单。

告诉对讲以聚焦视图:

View theView = findViewById(R.id.theViewId);
theView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);

就查找当前焦点节点而言,您希望在视图层次结构中搜索具有焦点的视图。

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static View findAccessibilityFocus(View view) {
if (view == null) return view;
if (view.isAccessibilityFocused()) return view;
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
View result = findAccessibilityFocus(childView);
if (result != null) return result;
}
}
return null;
}

使用此功能时必须小心! 当涉及竞争条件时,很难找到辅助功能焦点。 视图不必具有以辅助功能为中心的视图。 将辅助功能委托附加到根视图并跟踪具有辅助功能焦点的最后一个元素(跟踪相应的辅助功能事件)可能更容易。

最新更新