基于@Alan K 解决方案的替代代码组织。
创建类 DisableAutofillAction:
public class DisableAutofillAction implements ViewAction {
@Override
public Matcher<View> getConstraints() {
return Matchers.any(View.class);
}
@Override
public String getDescription() {
return "Dismissing autofill picker";
}
@Override
public void perform(UiController uiController, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AutofillManager autofillManager = view.getContext().getSystemService(AutofillManager.class);
if (autofillManager != null) {
autofillManager.cancel();
}
}
}
}
而且,在您的代码中,当您需要为 editTextPassword 禁用自动填充时...
editTextPassword.perform(..., ViewActions.closeSoftKeyboard(), DisableAutofillAction())