下面列出了android.support.v4.view.accessibility.AccessibilityNodeInfoCompat#isCheckable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns whether the supplied {@link View} and {@link AccessibilityNodeInfoCompat} would
* produce spoken feedback if it were accessibility focused. NOTE: not all speaking nodes are
* focusable.
*
* @param view The {@link View} to evaluate
* @param node The {@link AccessibilityNodeInfoCompat} to evaluate
* @return {@code true} if it meets the criterion for producing spoken feedback
*/
public static boolean isSpeakingNode(
@Nullable AccessibilityNodeInfoCompat node,
@Nullable View view) {
if (node == null || view == null) {
return false;
}
if (!node.isVisibleToUser()) {
return false;
}
int important = ViewCompat.getImportantForAccessibility(view);
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS ||
(important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO &&
node.getChildCount() <= 0)) {
return false;
}
return node.isCheckable() || hasText(node) || hasNonActionableSpeakingDescendants(node, view);
}
private static boolean isSpeakingNode(Context context, AccessibilityNodeInfoCompat node) {
if (hasText(node)) {
LogUtils.log(AccessibilityNodeInfoUtils.class, Log.VERBOSE,
"Speaking, has text");
return true;
}
// Special case for check boxes.
if (node.isCheckable()) {
LogUtils.log(AccessibilityNodeInfoUtils.class, Log.VERBOSE,
"Speaking, is checkable");
return true;
}
// Special case for web content.
if (WebInterfaceUtils.supportsWebActions(node)) {
LogUtils.log(AccessibilityNodeInfoUtils.class, Log.VERBOSE,
"Speaking, has web content");
return true;
}
// Special case for containers with non-focusable content.
if (hasNonActionableSpeakingChildren(context, node)) {
LogUtils.log(AccessibilityNodeInfoUtils.class, Log.VERBOSE,
"Speaking, has non-actionable speaking children");
return true;
}
return false;
}
@Override
public void format(Editable result,
Context context,
AccessibilityNodeInfoCompat node) {
int oldLength = result.length();
CharSequence text = null;
AccessibilityNodeInfoCompat labeledBy = node.getLabeledBy();
if (labeledBy != null) {
text = LabelingUtils.getNodeText(labeledBy, getLabelManager());
labeledBy.recycle();
}
if (text == null) {
text = LabelingUtils.getNodeText(node, getLabelManager());
}
if (text == null) {
text = "";
}
result.append(text);
if (node.isCheckable() || node.isChecked()) {
CharSequence mark;
if (node.isChecked()) {
mark = context.getString(R.string.checkmark_checked);
} else {
mark = context.getString(R.string.checkmark_not_checked);
}
StringUtils.appendWithSpaces(result, mark);
}
if (oldLength == result.length()
&& AccessibilityNodeInfoUtils.isActionableForAccessibility(
node)) {
result.append(getFallbackText(context, node));
}
}
/**
* Gets a description of the properties of a node.
*/
public static CharSequence nodeDebugDescription(AccessibilityNodeInfoCompat node) {
StringBuilder sb = new StringBuilder();
sb.append(node.getWindowId());
if (node.getClassName() != null) {
appendSimpleName(sb, node.getClassName());
} else {
sb.append("??");
}
if (!node.isVisibleToUser()) {
sb.append(":invisible");
}
if (node.getText() != null) {
sb.append(":");
sb.append(node.getText().toString().trim());
}
if (node.getContentDescription() != null) {
sb.append(":");
sb.append(node.getContentDescription().toString().trim());
}
int actions = node.getActions();
if (actions != 0) {
sb.append(":");
if ((actions & AccessibilityNodeInfoCompat.ACTION_FOCUS) != 0) {
sb.append("F");
}
if ((actions & AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS) != 0) {
sb.append("A");
}
if ((actions & AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS) != 0) {
sb.append("a");
}
if ((actions & AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD) != 0) {
sb.append("-");
}
if ((actions & AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD) != 0) {
sb.append("+");
}
}
if (node.isCheckable()) {
sb.append(":");
if (node.isChecked()) {
sb.append("(X)");
} else {
sb.append("( )");
}
}
if (node.isFocusable()) {
sb.append(":focusable");
}
if (node.isFocused()) {
sb.append(":focused");
}
if (node.isSelected()) {
sb.append(":selected");
}
if (node.isClickable()) {
sb.append(":clickable");
}
if (node.isLongClickable()) {
sb.append(":longClickable");
}
if (node.isAccessibilityFocused()) {
sb.append(":accessibilityFocused");
}
if (!node.isEnabled()) {
sb.append(":disabled");
}
return sb.toString();
}
@Nullable
public static String getFocusableReasons(View view) {
AccessibilityNodeInfoCompat node = createNodeInfoFromView(view);
try {
boolean hasText = AccessibilityUtil.hasText(node);
boolean isCheckable = node.isCheckable();
boolean hasNonActionableSpeakingDescendants =
AccessibilityUtil.hasNonActionableSpeakingDescendants(node, view);
if (AccessibilityUtil.isActionableForAccessibility(node)) {
if (node.getChildCount() <= 0) {
return "View is actionable and has no children.";
} else if (hasText) {
return "View is actionable and has a description.";
} else if (isCheckable) {
return "View is actionable and checkable.";
} else if (hasNonActionableSpeakingDescendants) {
return "View is actionable and has non-actionable descendants with descriptions.";
}
}
if (AccessibilityUtil.isTopLevelScrollItem(node, view)) {
if (hasText) {
return "View is a direct child of a scrollable container and has a description.";
} else if (isCheckable) {
return "View is a direct child of a scrollable container and is checkable.";
} else if (hasNonActionableSpeakingDescendants) {
return
"View is a direct child of a scrollable container and has non-actionable " +
"descendants with descriptions.";
}
}
if (hasText) {
return "View has a description and is not actionable, but has no actionable ancestor.";
}
return null;
} finally {
node.recycle();
}
}