Можно ли получить атрибуты пользовательского представления с помощью служб доступности - PullRequest
0 голосов
/ 01 июля 2019

У меня есть пользовательское представление с атрибутом String "abc"

public class MyCustomView extends View {
    private String abc;
}

Можно ли с помощью службы специальных возможностей получить атрибут "abc", когда на экране пользователя отображается "MyCustomView"?

1 Ответ

0 голосов
/ 12 июля 2019
public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId (String viewId);

Вы можете найти узел вашего пользовательского представления по идентификатору или тексту

public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText (String text);

Вы можете найти такие методы, как

private List<CharSequence> getAllChildNodeText(AccessibilityNodeInfoCompat infoCompat) {
    List<CharSequence> contents = new ArrayList<>();
    if (infoCompat == null)
        return contents;
    if (infoCompat.getContentDescription() != null) {
        contents.add(infoCompat.getContentDescription().toString().isEmpty() ? "unlabelled" : infoCompat.getContentDescription());
    } else if (infoCompat.getText() != null) {
        contents.add(infoCompat.getText().toString().isEmpty() ? "unlabelled" : infoCompat.getText());
    } else {
        getTextInChildren(infoCompat, contents);
    }
    if (infoCompat.isClickable()) {
        if (infoCompat.getClassName().toString().contains(Button.class.getSimpleName())) {
            if (contents.size() == 0) {
                contents.add("Unlabelled button");
            } else {
                contents.add("button");
            }
        }
        contents.add("Double tap to activate");
    }
    return contents;
}


private void getTextInChildren(AccessibilityNodeInfoCompat nodeInfoCompat, List<CharSequence> contents) {
    if (nodeInfoCompat == null)
        return;
    if (!nodeInfoCompat.isScrollable()) {
        if (nodeInfoCompat.getContentDescription() != null) {
            contents.add(nodeInfoCompat.getContentDescription());
        } else if (nodeInfoCompat.getText() != null) {
            contents.add(nodeInfoCompat.getText());
        }
        if (nodeInfoCompat.getChildCount() > 0) {
            for (int i = 0; i < nodeInfoCompat.getChildCount(); i++) {
                if (nodeInfoCompat.getChild(i) != null) {
                    getTextInChildren(nodeInfoCompat.getChild(i), contents);
                }
            }
        }
    }
}
...