简体   繁体   English

无法将可访问性焦点设置在标题文本视图上

[英]Unable to set accessibility focus on title textview

I have an EditText on an activity that requests focus when the activity launches.我有一个活动的 EditText,该活动在活动启动时请求焦点。 This is great for users who do not have TalkBack enabled.这对于没有启用 TalkBack 的用户来说非常有用。 However, for accessibility (ie for users who have TalkBack enabled), I need to set the focus on the titlebar of the screen, instead of the EditText.但是,对于可访问性(即对于启用 TalkBack 的用户),我需要将焦点设置在屏幕的标题栏上,而不是 EditText。

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):我已经尝试了几件事,但是在将焦点设置在标题栏的 textview 上没有成功(我已经检查过 titleTextView 没有返回 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);          
    }

The titleTextView never receives focus when TalkBack is on.当 TalkBack 开启时,titleTextView 永远不会获得焦点。

Any help would be appreciated.任何帮助将不胜感激。

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.我能够通过将代码移动到 if 块中来解决这个问题,如果用户启用了 TalkBack,则只执行块中的代码。 Namely, accessibilityManager.isEnabled() will only evaluate to true if TalkBack is on:也就是说,如果 TalkBack 开启,accessibilityManager.isEnabled() 只会评估为 true:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM