【发布时间】:2017-06-20 23:40:59
【问题描述】:
我正在使用AccessibilityService 在其他应用程序或浏览器中抓取字段。
为了获取字段的文本,我在下面有这个 sn-p 代码:
AccessibilityNodeInfo node;
text = node.getText().toString();
但是,在某些字段中,特别是在浏览器中,如果字段为空,则给我该字段的提示文本,并且我无法从提示值中删除文本值。
我知道在 Android O 中,我们将为此提供 AccessibilityNodeInfo.getHintText(),这很酷,但我需要在以前的 Android 版本中使用我的应用程序。
有人对我的情况有什么解决办法吗?!
***更新 我已经尝试过以下解决方案来实现我的目标:
/** Ummm, didn't work :( */
public static boolean isTextViewWithHint(AccessibilityNodeInfo node)
{
boolean isHint;
String text, hint;
text = node.getText().toString();
Log.d(TAG, "text = " + text);
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "");
node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
Log.d(TAG, "node.refresh() = " + node.refresh());
hint = node.getText().toString();
Log.d(TAG, "hint = " + hint);
if (text != null && hint != null && text.equals(hint))
{
// current value of node is a Hint!
Log.d(TAG, text + " is a Hint.");
isHint = true;
}
else
{
Log.d(TAG, text + " is real value.");
isHint = false;
}
Bundle arguments2 = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, text);
node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments2);
return isHint;
}
但这不起作用,因为当我清除该字段时,hint = node.getText().toString(); 仍然给我与text = node.getText().toString(); 之前给我相同的值!不知道为什么?!
如果hint = node.getText().toString(); 行(在我清除字段后)给了我字段的提示文本,或者空值,我的问题就解决了。
有人看到我上面的 sn-p 代码有什么问题吗?!
【问题讨论】:
-
请不要降级我的问题:)!我问谁在 android 中与
AccessibilityService一起工作过。另外,对不起我的英语。 -
如果您将文本设置为 null 而不是空,您的“破解”解决方案可能会起作用。
标签: android accessibilityservice