Android - 将 TalkBack 可访问性焦点设置为特定视图 [英] Android - Set TalkBack accessibility focus to a specific view

查看:41
本文介绍了Android - 将 TalkBack 可访问性焦点设置为特定视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启用 TalkBack 后,是否可以将辅助功能焦点手册设置为特定视图?例如,当我的 Activity 启动时,我希望 TalkBack 自动关注某个按钮(视图周围的黄色框)并阅读其内容描述.

When TalkBack is enabled, is there any way to set the accessibility focus manual to a specific view? For instance, when my Activity is started I want TalkBack to automatically focus on a certain Button (yellow frame around the view) and read its content description.

到目前为止我尝试过的:

What I've tried so far:

    myButton.setFocusable(true);
    myButton.setFocusableInTouchMode(true);
    myButton.requestFocus();

requestFocus() 似乎只是请求输入焦点,与可访问性焦点无关.我也试过:

requestFocus(), it seems, is only requesting input focus and has nothing to do with accessibility focus. I've also tried:

    myButton.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    myButton.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
    myButton.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
    myButton.announceForAccessibility("accessibility test");
    myButton.performAccessibilityAction(64, null); // Equivalent to ACTION_ACCESSIBILITY_FOCUS

    AccessibilityManager manager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (manager.isEnabled()) {
        AccessibilityEvent e = AccessibilityEvent.obtain();
        e.setSource(myButton);
        e.setEventType(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
        e.setClassName(getClass().getName());
        e.setPackageName(getPackageName());
        e.getText().add("another accessibility test");
        manager.sendAccessibilityEvent(e);
    }

这些似乎都不起作用.

推荐答案

免责声明:强制将 Activity 负载放在任何位置,但始终位于顶部栏之外(好吧,几乎永远不应该说),但实际上,只是不要不要这样做.它违反了 WCAG 2.0 的各种规定,包括 3.2.1 和 3.2.3,分别关于可预测的导航和上下文变化.这样做很可能实际上使您的应用更难以访问.

DISCLAIMER: Forcing focus on Activity load to be anywhere but at the top bar is always (okay, always should almost never be said), but really, just don't do it. It is a violation of all sorts of WCAG 2.0 regulations, including 3.2.1 and 3.2.3, regarding predictable navigation and context changes respectively. You are, likely, actually making your app MORE inaccessible by doing this.

http://www.w3.org/TR/WCAG20/#consistent-行为

结束免责声明.

您正在使用正确的函数调用.您需要做的就是:

You are using the correct function calls. All you should need to do is this:

myButton.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);

问题更可能是您尝试执行此操作的时间点.Talkback 会在活动周期稍晚的时候将其自身附加到您的活动中.以下解决方案说明了这个问题,老实说,我不确定是否有更好的方法来做到这一点.我尝试了 onPostResume,这是 Android 操作系统调用的最后一个回调,关于活动的加载,但我仍然不得不添加延迟.

The problem is more likely the point at which your attempting to do this. Talkback attaches itself to your activity slightly later in the activity cycle. The following solution illustrates this problem, I'm not sure there is a better way to do this, to be honest. I tried onPostResume, which is the last callback the Android OS calls, regarding the loading of activities, and still I had to add in a delay.

@Override
protected void onPostResume() {
    super.onPostResume();

    Log.wtf(this.getClass().getSimpleName(), "onPostResume");

    Runnable task = new Runnable() {

        @Override
        public void run() {
            Button theButton = (Button)WelcomeScreen.this.findViewById(R.id.idButton);
            theButton.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
        }
    };

    final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();

    worker.schedule(task, 5, TimeUnit.SECONDS);

}

您或许可以创建自定义视图.视图中的回调可以提供您执行此操作所需的逻辑,而没有竞争条件!如果我有时间,我可能会稍后再研究一下.

You might be able to create a custom view. The callbacks within view, may provide the logic you need to do this, without the race condition! I might look into it more later if I get time.

这篇关于Android - 将 TalkBack 可访问性焦点设置为特定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆