我在 Android 中创建了一个 AccessibilityService,我想在关注页面上的 EditText 后显示一个 AlertDialog(注意:EditText 可能来自另一个应用程序,例如登录屏幕,所以我并不总是知道 ID)。当您单击 AlertDialog 中的“确认”按钮时,它会使用文本填充该 EditText。
除了最后一部分之外,我已经完成了所有步骤......我无法弄清楚如何用文本填充 EditText。我猜有一种方法可以在某处投射 findViewByID() 方法,但我不知道如何找到 EditText 的 ID(见上文,EditText 可能来自另一个应用程序)。请参阅下面的代码,我走得远吗?下面的代码总是错误地告诉我有关密封实例的问题(无法在未密封的实例上执行此操作。)。
public void onAccessibilityEvent(final AccessibilityEvent event) {
final AccessibilityNodeInfo source = event.getSource();
if ((event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED || event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) && CLASS_NAME_EDIT_TEXT.equals(event.getClassName())) {
AlertDialog.Builder mSuspendDialog = new AlertDialog.Builder(getApplicationContext())
.setTitle(R.string.str_have_password_question)
.setPositiveButton(R.string.str_decision_use_password, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (event.getSource() != null & event.getClassName().equals("android.widget.EditView")) {
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo
.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "newtexttopopulateedittext");
event.getSource().performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
}
}
})
.setNegativeButton(R.string.str_decision_close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog alert11 = mSuspendDialog.create();
// Ensure we can show the dialog from this service.
alert11.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert11.show();