0

我正在尝试从 AccessiblityNodeInfo 的所有子节点中读取文本。

我试着得到AccessiblityNodeInfo.getParent()AccessibilityNodeInfo.getChildCount()

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

如上所述访问子节点以从子节点获取文本。但它没有提供屏幕上的所有文本。

谁能建议我该怎么做才能得到文本?

4

1 回答 1

1
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);
                }
            }
        }
    }
}
于 2018-08-16T13:08:10.380 回答