как получить текст со всех дочерних узлов в AccessibilityNodeInfo - PullRequest
0 голосов
/ 30 мая 2018

Я пытаюсь прочитать текст со всех дочерних узлов AccessiblityNodeInfo.

Я попытался получить AccessiblityNodeInfo.getParent() и AccessibilityNodeInfo.getChildCount()

for(int i=0; i<accessibilityNodeInfo.getChildCount();i++){
accessibilityNodeInfo.getChild(i).getText();
}

Получил доступ к дочерним узлам, как указано выше, чтобы получить текст от дочерних узлов.но он не предоставляет весь текст на экране.

Может кто-нибудь подсказать мне, что делать, чтобы получить текст?

1 Ответ

0 голосов
/ 16 августа 2018
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);
                }
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...