我想使用AccessibilityDelegateCompat
来调整AccessibilityEvent
的内容。
我已经看到了各种示例,但是我不确定为什么我不能在输出端附加声音。
这是完整的代码
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_more, container, false);
ViewCompat.setAccessibilityDelegate(mProfilePic,new AccessibilityDelegateCompat(){
@Override
public void sendAccessibilityEvent(View host, int eventType) {
super.sendAccessibilityEvent(host, eventType);
}
@Override
public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
event.getText().add("TEST");
super.sendAccessibilityEventUnchecked(host, event);
}
@Override
public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
event.getText().add("TEST");
return super.dispatchPopulateAccessibilityEvent(host, event);
}
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
event.getText().add("TEST");
super.onPopulateAccessibilityEvent(host, event);
event.getText().add("TEST");
}
@Override
public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
event.getText().add("TEST");
super.onInitializeAccessibilityEvent(host, event);
}
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
}
@Override
public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, AccessibilityEvent event) {
event.getText().add("TEST");
return super.onRequestSendAccessibilityEvent(host, child, event);
}
@Override
public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) {
return super.getAccessibilityNodeProvider(host);
}
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
return super.performAccessibilityAction(host, action, args);
}
});
return view;
}
如我所见,我尝试在各个地方添加我的内容。
请注意,mProfilePic
具有以XML设置的内容描述。我想用代码添加到现有的内容描述中。
答案 0 :(得分:1)
找到了似乎有效的解决方案。我在带有Google Talkback的Nexus 6p和使用Samsung Voice的Samsung S7上进行了测试。
ViewCompat.setAccessibilityDelegate(mProfilePic, new AccessibilityDelegateCompat() {
private boolean didPerformAccessibilityAction = false
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
if (didPerformAccessibilityAction) {
didPerformAccessibilityAction = false
info.contentDescription = "MY NEW CONTENT"
}
}
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
didPerformAccessibilityAction = super.performAccessibilityAction(host, action, args)
return didPerformAccessibilityAction
}
});
onInitializeAccessibilityNodeInfo
在最后一个之前被调用
可访问性事件已处理。在最后一个阶段,再次设置要发布的内容描述。
因此,任何其他委托方法所做的更改都无效,因为onInitializeAccessibilityNodeInfo
会覆盖它。
每个孩子都将调用此方法来初始化节点信息,因此在调用修饰符之前,我们需要一个阻止程序,直到发生真正的可访问性事件为止,这样我们才等到performAccessibilityAction
似乎在真实事件上被调用在我们进行修改之前。