AcessibilityService
中有一些功能可以在字段上插入字符,但不删除以前的内容以及模拟退格按钮?
现在我已经使用全局
String
变量来实现它,例如:
text += "My text or character here
- 插入text = text.substring(0, text.length() - 1)
- 退格键这不好,因为当需要跳转到下一个字段时,请先清除变量。
这是在@Pawan Singh Harariya 提出建议后如何实施。
public void keyboardText(String text) {
Bundle arguments = new Bundle();
AccessibilityNodeInfo node = findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
if (node != null) {
String nodeHint = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
nodeHint = String.valueOf(node.getHintText());
}
String nodeText = String.valueOf(node.getText());
if (nodeHint != null) {
if (text.equals("BKSP"))
nodeText = nodeText.substring(0, nodeText.length() - 1).replace(nodeHint, "");
else if (text.equals("SPCE"))
nodeText = nodeText.replace(nodeHint, "") + " ";
else nodeText = nodeText.replace(nodeHint, "") + text;
} else {
if (text.equals("BKSP"))
nodeText = nodeText.substring(0, nodeText.length() - 1);
else if (text.equals("SPCE"))
nodeText += " ";
else nodeText += text;
}
arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, nodeText);
node.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT, arguments);
}
}