• 【转】Android辅助功能AccessibilityService自动全选择文字粘贴模拟输入


    网上找了很久AccessibilityService全选文字的方法,一直没找到,自己研究了半天,分享出来。

    /**
         * 输入文本
         */
        public void inputText(List<String> viewIds, String text){
            AccessibilityNodeInfo root = getRootInActiveWindow();
            if(root == null) return;
     
            for (String id : viewIds){
                final List<AccessibilityNodeInfo> list = root.findAccessibilityNodeInfosByViewId(id);
                if(list != null && !list.isEmpty()){
                    AccessibilityNodeInfo info = list.get(0);
                    //粘贴板
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText("label", text);
                    clipboard.setPrimaryClip(clip);
     
                    CharSequence txt = info.getText();
                    if(txt == null) txt = "";
     
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        Bundle arguments = new Bundle();
                        arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, 0);
                        arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, txt.length());
                        info.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
                        info.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, arguments);
                        info.performAction(AccessibilityNodeInfo.ACTION_PASTE);
                    }
                    break;
                }
            }
        }
    

      


    上面这个方法使用了剪贴板是安卓4.3以上通用。
    下面这种方法是安卓5.0以上:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, txt);
        info.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }
    

      


    如果觉的有用,请在评论留下你的想法,感谢
    ---------------------
    from :https://blog.csdn.net/sinat_25689603/article/details/80265347

  • 相关阅读:
    java文件下载
    java程序运行原理
    java io流(核心:读进来,写出去)
    oracle操作表和字段的sql复习
    深入理解C/S和B/S模式
    Windows PyCharm永久激活
    MacBook PyCharm永久激活
    百度云同同步盘 mac版
    SJW-遍历我的账户左侧导航页面(句柄切换)
    python-selenium无法调用浏览器的问题==
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/10102055.html
Copyright © 2020-2023  润新知