1 package com.lihai.zapp.serv; 2 3 import java.util.List; 4 5 import android.accessibilityservice.AccessibilityService; 6 import android.annotation.SuppressLint; 7 import android.view.accessibility.AccessibilityEvent; 8 import android.view.accessibility.AccessibilityNodeInfo; 9 10 @SuppressLint("NewApi") 11 public class MyAccessibilityService extends AccessibilityService { 12 13 @Override 14 public void onCreate() { 15 super.onCreate(); 16 } 17 18 @Override 19 public void onAccessibilityEvent(AccessibilityEvent event) { 20 switch (event.getEventType()) { 21 case AccessibilityEvent.TYPE_VIEW_FOCUSED: 22 AccessibilityNodeInfo rootNodeInfo = getRootInActiveWindow(); 23 List<AccessibilityNodeInfo> nodeInfos = rootNodeInfo.findAccessibilityNodeInfosByText("Button"); 24 for (AccessibilityNodeInfo n : nodeInfos) { 25 n.setText("Change"); 26 } 27 break; 28 } 29 } 30 31 @Override 32 public void onInterrupt() { 33 } 34 }
在这里我想做的是一个当用户开启了辅助功能后能够在我想监听的应用中去改变该应用中一个Button控件的显示文本。但是会报错。求解,setText() 方法的正确使用方式
API>=21
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "Change");
n.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);