前言
縱觀APP這片海洋,支持無(wú)障礙模式的不是很多揖闸,網(wǎng)上可供參考的資料少之又少揍堕,只能依靠強(qiáng)大的Debug不斷嘗試,最后挖掘出一點(diǎn)精華封裝成工具類(lèi)供大家使用汤纸,納入知識(shí)庫(kù)衩茸。
public class BarrierfreeUtils {
/**
* 設(shè)置無(wú)障礙焦點(diǎn)
* 若關(guān)閉,用戶(hù)控件將失去焦點(diǎn)贮泞,不會(huì)播報(bào)內(nèi)容
* @param view 指定控件
* @param focused true打開(kāi)楞慈,false關(guān)閉
*/
public static void setAccessibilityFocusable(View view, boolean focused) {
if (android.os.Build.VERSION.SDK_INT >= 16) {
if (focused) {
ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
} else {
ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
}
}
}
/**
* 動(dòng)態(tài)設(shè)置指定控件的播報(bào)內(nèi)容
* @param view 指定控件l
* @param contentDesc 播報(bào)語(yǔ)音內(nèi)容
*/
public static void setAccessibilityDesc(final View view, final String contentDesc) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
view.setAccessibilityDelegate(new View.AccessibilityDelegate() {
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(host, info);
if (contentDesc != null) {
info.setContentDescription(contentDesc);
}
info.setClassName(view.getClass().getName());
}
});
}
}
/**
* 定位指定控件播放語(yǔ)音內(nèi)容
* @param view 指定控件
*/
public static void obtainBlindModeFocusable(final View view) {
view.postDelayed(new Runnable() {
@Override
public void run() {
if (android.os.Build.VERSION.SDK_INT >= 15) {
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
}
}
}, 100);
}
/**
* 始終播報(bào)語(yǔ)音內(nèi)容,不用定位焦點(diǎn)
* @param view
* @param desc 描述內(nèi)容啃擦,將它放到event的getText()中囊蓝,然后請(qǐng)求View的父類(lèi)來(lái)發(fā)出事件
* @desc ccessibilityEvent.TYPE_ANNOUNCEMENT是代表元素需要TalkBack服務(wù)來(lái)讀出描述內(nèi)容。
*/
public static void setSuperclassFocusableDesc(final View view, final String desc) {
view.postDelayed(new Runnable() {
@Override
public void run() {
if(android.os.Build.VERSION.SDK_INT >= 16){
AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
event.setPackageName(view.getContext().getPackageName());
event.setClassName(view.getClass().getName());
event.setSource(view);
event.getText().add(desc);
view.getParent().requestSendAccessibilityEvent(view, event);
}
}
}, 100);
}
/**
* 經(jīng)試用無(wú)效令蛉, 還請(qǐng)大神賜教
* @param view
* @param desc
*/
public static void setDialogUnbroadcast(final View view) {
if (android.os.Build.VERSION.SDK_INT >= 15) {
view.setAccessibilityDelegate(new View.AccessibilityDelegate() {
@Override
public void sendAccessibilityEvent(View host, int eventType) {
// 彈出Popup或dialog后聚霜,不自動(dòng)讀各項(xiàng)內(nèi)容
if (eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
super.sendAccessibilityEvent(host, eventType);
}
}
});
}
}
}
另外還可以通過(guò)延遲的方式,執(zhí)行播報(bào)的順序言询,例如:
delImg.setContentDescription("下載圖片");
delImg.postDelayed(new Runnable() {
@Override
public void run() {
delImg.setContentDescription("關(guān)閉頁(yè)面");
}
}, 2000);
參考好文:
http://geek.csdn.net/news/detail/93269
http://www.voidcn.com/article/p-raugqcll-rp.html