我正在尝试使用AccessibilityManager和TalkBack触发自定义AccessibilityEvent
。
事件的用例是当用户单击操作栏时,片段轮询对象列表,然后根据列表的大小简化其AccessibilityEvent内容。
当我尝试运行时,我没有得到预期的TalkBack消息。我很确定我缺少一些基本的实例化AccessibilityEvent。
我也不确定我是否需要使用,或者如何在此处应用AccessibilityDelegate
,因为回调来自MenuItem
而不是View
。我知道我可以调用findViewById
来获取这个MenuItem的视图,但我对这些API知之甚少。
关于这两点的任何指导都会很棒!
有问题的问题基本上由以下伪代码描述:
public class MyFragment extends Fragment {
//...
private List<Pojo> mPojoList;
//...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.the_id_for_my_menuitem) {
if (booleanCheck() && !mPojoList.isEmpty()) {
//create the Accessibility event
final AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_CLICKED);
event.setContentDescription(String.format("deleting %2d pojos", mPojoList.size()));
//Send a custom accessibility event to let the user know that we're deleting X objects.
final AccessibilityManager mgr = (AccessibilityManager) this.getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE);
//PROBLEM: We're not seeing this event come through in TalkBack.
mgr.sendAccessibilityEvent(event);
//Delete the objects.
myDeleteObjectsFunction();
}
}
}}
答案 0 :(得分:2)
尝试使用View对象触发辅助功能事件。
AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_CLICKED);
event.setContentDescription(String.format("deleting %2d pojos", mPojoList.size()));
View view = getActivity().findViewById(R.id.child_view);
ViewParent parent = view.getParent();
if (parent != null) {
parent.requestSendAccessibilityEvent(view, event);
}
答案 1 :(得分:1)
虽然这是一个老问题,但我会发表我的答案,因为之前给出的答案对我不起作用。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.the_id_for_my_menuitem) {
if (booleanCheck() && !mPojoList.isEmpty()) {
AccessibilityManager manager = (AccessibilityManager)this.getSystemService(Context.ACCESSIBILITY_SERVICE);
if(manager.isEnabled()){
AccessibilityEvent event = AccessibilityEvent.obtain();
event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
event.setClassName(getClass().getName());
event.getText().add(*yourString*);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
event.setSource(findViewById(*yourButton*));
}
manager.sendAccessibilityEvent(event);
}
}
}
}