在字段上插入或删除字符而不删除之前的内容

问题描述 投票:0回答:1

AcessibilityService
中有一些功能可以在字段上插入字符,但不删除以前的内容以及模拟退格按钮?

现在我已经使用全局

String
变量来实现它,例如:

  • text += "My text or character here
    - 插入
  • text = text.substring(0, text.length() - 1)
    - 退格键

这不好,因为当需要跳转到下一个字段时,请先清除变量。

android accessibilityservice
1个回答
0
投票

这是在@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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.