网上找了很久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.****.net/sinat_25689603/article/details/80265347