编辑:我现在可以获取EditText文本值。这就是我所需要的。
我正在尝试使用Android无障碍服务访问网页元素,但我所能做的就是获取URL,而不是页面上的任何文本。实际上是否可以从网页上的UI元素中删除文本?
我尝试使用getRootInActiveWindow访问根节点,然后使用getChild()递归遍历根树并打印出我遇到的所有文本。
我的递归方法:
public void getChildrenInfo(AccessibilityNodeInfo info) {
try {
if (info == null) {
logOther("info is null");
return;
}
if (info.getText() != null && info.getText().length() > 0) {
String nodeText = info.getText().toString();
log("node has text:",nodeText);
}
if(info.getContentDescription() != null){
log("node content info:",info.getContentDescription().toString());
}
//logOther("nodeinfo child count is:", String.valueOf(info.getChildCount()));
for (int i = 0; i < info.getChildCount(); i++) {
AccessibilityNodeInfo child = info.getChild(i);
logOther("got child:", String.valueOf(i));
String className = "";
if(child.getClass() != null){
className = child.getClassName().toString();
//log("child", String.valueOf(i),"classType is:", className);
}
else{
//log("child", String.valueOf(i),"classType is null");
}
if(child != null){
if(child.getChildCount() > 0){
//logOther("Has children. Getting childrenInfo");
getChildrenInfo(child);
}
else if(child.getText() != null && child.getText().length() > 0){
String nodeText = child.getText().toString();
log("child has text:",nodeText);
}
child.recycle();
}
}
} catch(StackOverflowError ex){
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}