• 【转】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

  • 相关阅读:
    C99新增内容之复合文字(compound literal)
    直接编译caffe出现的两个问题
    安装cuda8.0中所遇到的问题-解决办法
    windows环境Caffe安装配置步骤(无GPU)及mnist训练
    leetcodeTop100好题
    只不过是从头再来,读java源码
    文章收藏
    java代码优化技巧
    xshell
    MVC框架笔记
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/10102055.html
Copyright © 2020-2023  润新知