我想在辅助功能服务中更改 EditText 内的文本。我的代码是:
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final AccessibilityNodeInfo source = event.getSource();
if (source != null & event.getClassName().equals("android.widget.EditText")) {
AccessibilityNodeInfo currentSource = source;
ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "TestData");
clipboard.setPrimaryClip(clip);
currentSource.performAction(AccessibilityNodeInfo.ACTION_PASTE);
而且效果很好。当用户按下 EditText 时,它会变为“TestData”。之后显示带有按钮的对话框。我想通过按下按钮来更改文本。
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Test dialog");
builder.setMessage("Content");
builder.setPositiveButton("User", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ClipboardManager clipboard = (ClipboardManager) getApplication().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "Test Username");
clipboard.setPrimaryClip(clip);
currentSource.performAction(AccessibilityNodeInfo.ACTION_PASTE);
dialog.dismiss();
}
});
如果按下按钮,剪贴板会刷新,但 EditText 内的文本不会。