0

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

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

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

我什至对本机设置应用程序-wifi 页面和“ES 文件浏览”等现有应用程序进行了一些研究,在这些应用程序中,当用户将方向更改为横向模式时,也无法保持可访问性。系统正在从横向选择一些随机元素到纵向,反之亦然。

下面是代码片段

可访问性_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>

可访问性SampleActivity.java

公共类 AccessibilitySampleActivity 扩展 Activity {

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);
        }
    }
}

}

对于像 TextView、Button、Checkbox 这样的普通视图,它能够在用户旋转屏幕时保持状态。

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

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

1 回答 1

0

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

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

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

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

总之,从可访问性的角度来看,您尝试做的事情确实有些误导,实际上会使事情变得更糟。话虽如此,在 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;
}

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

于 2017-05-09T17:50:06.607 回答