[英]how to get text from all the child nodes in AccessibilityNodeInfo
I'm trying to read the text from all the child nodes of an AccessiblityNodeInfo. 我正在尝试从AccessiblityNodeInfo的所有子节点读取文本。
I tried getting AccessiblityNodeInfo.getParent()
and AccessibilityNodeInfo.getChildCount()
我尝试获取AccessiblityNodeInfo.getParent()
和AccessibilityNodeInfo.getChildCount()
for(int i=0; i<accessibilityNodeInfo.getChildCount();i++){
accessibilityNodeInfo.getChild(i).getText();
}
Accessed the child nodes as mentioned above to get the text from the child nodes. 如上所述访问子节点以从子节点获取文本。 but it is not providing the all the text on the screen. 但它没有提供屏幕上的所有文本。
Can anyone suggest me what to do get the 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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.