【轉】Android輔助功能AccessibilityService自動全選擇文字粘貼模擬輸入

網上找了好久AccessibilityService全選文字的方法,一直沒找到,本身研究了半天,分享出來。java

/**
     * 輸入文本
     */
    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以上:ui

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.net

相關文章
相關標籤/搜索