gpt4 book ai didi

android - 无法在标题 TextView 上设置辅助功能焦点

转载 作者:行者123 更新时间:2023-11-29 19:42:21 24 4
gpt4 key购买 nike

我在 Activity 上有一个 EditText,它在 Activity 启动时请求焦点。这对于没有启用 TalkBack 的用户来说非常有用。但是,对于可访问性(即对于启用了 TalkBack 的用户),我需要将焦点设置在屏幕的标题栏上,而不是 EditText。

我已经尝试了几种方法,但未能成功将焦点设置在标题栏的 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);
}

当 TalkBack 打开时,titleTextView 永远不会获得焦点。

如有任何帮助,我们将不胜感激。

最佳答案

我能够通过将代码移动到 if block 中来解决此问题,以便仅在用户启用了 TalkBack 时才执行该 block 中的代码。也就是说,accessibilityManager.isEnabled() 只会在 TalkBack 开启时评估为真:

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

关于android - 无法在标题 TextView 上设置辅助功能焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com