无法将可访问性焦点设置为标题textview [英] Unable to set accessibility focus on title textview

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

问题描述

我在一个活动上有一个EditText,当活动启动时,它请求焦点。这对于未启用话语提示的用户来说非常有用。但是,为了实现可访问性(即对于启用了话语提示的用户),我需要将焦点设置在屏幕的标题栏上,而不是EditText上。

I have an EditText on an activity that requests focus when the activity launches. This is great for users who do not have TalkBack enabled. However, for accessibility (i.e. for users who have TalkBack enabled), I need to set the focus on the titlebar of the screen, instead of the EditText.

我尝试了几件事,但是未能将焦点放在标题栏的textview上(我检查了titleTextView不会返回null):

I have tried several things but have been unsuccessful at setting the focus on the titlebar's textview (I have checked that titleTextView does not return null):

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TextView titleTextView = null;
//get the tool bar's title TextView
try
{
    Class<?> toolbarClass = Toolbar.class;
    Field titleTextViewField = toolbarClass.getDeclaredField("mTitleTextView");
    titleTextViewField.setAccessible(true);
    titleTextView = (TextView)titleTextViewField.get(toolbar);
}
catch (NoSuchFieldException | IllegalAccessException e)
{
    e.printStackTrace();
}

if (titleTextView != null)
{
    titleTextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
    AccessibilityManager manager = (AccessibilityManager)getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (manager.isEnabled())
    {
        AccessibilityEvent e = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_FOCUSED);
        e.setSource(titleTextView);
        e.setEventType(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
        e.setClassName(getClass().getName());
        e.setPackageName(getPackageName());
        e.getText().add("Message");
        manager.sendAccessibilityEvent(e);          
    }

启用话语提示时,titleTextView永远不会获得焦点。

The titleTextView never receives focus when TalkBack is on.

任何帮助将不胜感激。

推荐答案

我能够通过移动代码来解决此问题如果用户启用了话语提示,则将其插入if块以仅执行该块中的代码。即,仅在启用TalkBack时,accessibilityManager.isEnabled()的计算结果才为true:

I was able to fix this by moving the code into the if block to only execute the code in the block if the user has TalkBack enabled. Namely, accessibilityManager.isEnabled() will only evaluate to true if TalkBack is on:

AccessibilityManager am = (AccessibilityManager)getSystemService(Context.ACCESSIBILITY_SERVICE);
if (am.isEnabled())
{
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(actionBar);
    toolbar.setFocusable(true);
    toolbar.setFocusableInTouchMode(true);
    try
    {               
        Class<?> toolbarClass = Toolbar.class;
        Field titleTextViewField = toolbarClass.getDeclaredField("mTitleTextView");
        titleTextViewField.setAccessible(true);
        TextView titleTextView = (TextView)titleTextViewField.get(toolbar);
        titleTextView.setFocusable(true);
        titleTextView.setFocusableInTouchMode(true);
    }
    catch (NoSuchFieldException | IllegalAccessException e)
    {               
        e.printStackTrace();            
    }
    toolbar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    AccessibilityEvent e = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    e.setSource(toolbar);
    e.setEventType(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    e.setClassName(getClass().getName());
    e.setPackageName(getPackageName());
    am.sendAccessibilityEvent(e);
}

这篇关于无法将可访问性焦点设置为标题textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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