关于Android的Accessibility--自定义View
自定义View
自定义view要处理的accessibility细节
- 处理方向控制;
处理keyevent中的KEYCODE_ENTER和KEYCODE_DPAD_CENTER - 实现accessibility api;
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED)
sendAccessibilityEventUnchecked()
dispatchPopulateAccessibilityEvent()
…(全是view中的方法) - 发送AccessibilityEvent对象;
view已经实现点击、长点、聚焦、滑动、HOVER_ENTER、HOVER_EXIT事件
自定义其他事件,比如slidebar改变数值的时候,需要发送TYPE_VIEW_TEXT_CHANGED - Populate AccessibilityEvent and AccessibilityNodeInfo
event包含很多属性,如类名和事件时间,是已经集成的。但是文字之类的需要自己合入,这些文字还是应当尽量简洁@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
// Call the super implementation to populate its text to the event, which
// calls onPopulateAccessibilityEvent() on API Level 14 and up.
boolean completed = super.dispatchPopulateAccessibilityEvent(event);
// In case this is running on a API revision earlier that 14, check// the text content of the event and add an appropriate text// description for this custom view:CharSequence text = getText();if (!TextUtils.isEmpty(text)) { event.getText().add(text); return true;}return completed;
}
//4.0以上
onPopulateAccessibilityEvent()添加或修改文字内容
onInitializeAccessibilityEvent() 添加事件状态,如是否选中
//额外处理
onInitializeAccessibilityNodeInfo()
ViewCompat.setAccessibilityDelegate(new AccessibilityDelegateCompat() {
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
super.onPopulateAccessibilityEvent(host, event);
// We call the super implementation to populate its text for the
// event. Then we add our text not present in a super class.
// Very often you only need to add the text for the custom view.
CharSequence text = getText();
if (!TextUtils.isEmpty(text)) {
event.getText().add(text);
}
}
@Override
public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(host, event);
// We call the super implementation to let super classes
// set appropriate event properties. Then we add the new property
// (checked) which is not supported by a super class.
event.setChecked(isChecked());
}
@Override
public void onInitializeAccessibilityNodeInfo(View host,
AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
// We call the super implementation to let super classes set
// appropriate info properties. Then we add our properties
// (checkable and checked) which are not supported by a super class.
info.setCheckable(true);
info.setChecked(isChecked());
// Quite often you only need to add the text for the custom view.
CharSequence text = getText();
if (!TextUtils.isEmpty(text)) {
info.setText(text);
}
}
}
如果自定义view,把内部切割成了多个区域,那么Android肯定也不能自动获得具体的区域参数。 此时需要建立一个虚拟的view结构,实现view.getAccessibilityNodeProvider(), ViewCompat.getAccessibilityNodeProvider() - 处理自定义touch event 对于一些特殊需求,对touch event的处理,可能不太符合常规的点击和长点逻辑,此时我们需要单独处理 首先需要生成一个AccessibilityEvent来处理click操作,然后要让accessibility service能够执行这个操作 模式如下:
class CustomTouchView extends View {
public CustomTouchView(Context context) { super(context);}boolean mDownTouch = false;@Overridepublic boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); // Listening for the down and up touch events switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mDownTouch = true; return true; case MotionEvent.ACTION_UP: if (mDownTouch) { mDownTouch = false; performClick(); // Call this method to handle the response, and // thereby enable accessibility services to // perform this action for a user who cannot // click the touchscreen. return true; } } return false; // Return false for other touch events}@Overridepublic boolean performClick() { // Calls the super implementation, which generates an AccessibilityEvent // and calls the onClick() listener on the view, if any super.performClick(); // Handle the action for the custom click here return true;}
}
更多相关文章
- Android下的Touch事件分发
- android bitmap绘制文字自动换行
- 在android4.2中添加自己的intent事件并使用
- android Spinner点击事件处理
- Android studio 学习1:实现点击事件的4种方法
- Android事件分发机制完全解析 ontouch
- android studio 添加按钮点击事件的三种方法
- android 按钮 背景 文字 自定义
- ListView中item点击事件、item保持选中状态以及其他细节