问题
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.
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);
}
The titleTextView never receives focus when TalkBack is on.
Any help would be appreciated.
回答1:
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);
}
回答2:
In my case this works (kotlin):
view.post {
this.isFocusable = true
this.isFocusableInTouchMode = true
this.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
}
来源:https://stackoverflow.com/questions/38447493/unable-to-set-accessibility-focus-on-title-textview