Android O具有支持字段自动填充的功能。是否有任何方法可以禁用特定应用程序的此功能?也就是说,我想强制我的应用程序不使用自动填充服务。
这可能吗?
要阻止整个活动的自动填充,请在活动的onCreate()中使用以下内容:
getWindow()
.getDecorView()
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
有比这更好的方法吗?
Android O具有支持字段自动填充的功能。是否有任何方法可以禁用特定应用程序的此功能?也就是说,我想强制我的应用程序不使用自动填充服务。
这可能吗?
要阻止整个活动的自动填充,请在活动的onCreate()中使用以下内容:
getWindow()
.getDecorView()
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
目前还没有一种直接的方法可以禁用整个应用程序的自动填充,因为自动填充功能是视图特定的。
您仍然可以尝试这种方式,并在各处调用BaseActivity。
public class BaseActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disableAutofill();
}
@TargetApi(Build.VERSION_CODES.O)
private void disableAutofill() {
getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
}
你也可以通过这种方式强制请求自动填充。public void forceAutofill() {
AutofillManager afm = context.getSystemService(AutofillManager.class);
if (afm != null) {
afm.requestAutofill();
}
}
注意:目前自动填充功能仅适用于API 26 Android Oreo 8.0
希望这能有所帮助!
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object reference
。 - Someone Somewhere我认为被接受的答案是不正确的:
所以我有自己的类,它是 继承自 android.support.v7.widget.AppCompatEditText,我所做的就是用下面的值重写了以下方法:
@Override
public int getAutofillType() {
return AUTOFILL_TYPE_NONE;
}
没有其他解决方案有效,甚至包括android:importantForAutofill="no"。
getAutofillType()来自View类,因此它也适用于TextInputEditText等其他类!
android:importantForAutofill="no"
或android:importantForAutofill="noExcludeDescendants"
无效。已在Oreo 8.0(API级别26)和Android Pie(API级别28)模拟器上测试。 - Carmen我也遇到了这个问题。原因是在TextInputLayout中嵌套的EditText上设置了提示文本。
经过一些研究,我在2017年6月发布的26.0.0 Beta 2版本说明中找到了一个有价值的信息。 Andorid Support Release Notes June 2017
TextInputLayout必须在onProvideAutofillStructure()上设置提示信息。
这使我尝试在TextInputLayout上设置提示,取代了嵌套的EditText上的提示。
这样对我解决了崩溃问题。 例如:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Some Hint Text"
android.support.design:hintAnimationEnabled="true"
android.support.design:hintEnabled="true"
android.support.design:layout_marginTop="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
android.support.design
开头的行,只需移动提示参数即可。 - Someone Somewherevalues-v26/styles.xml
文件中添加以下样式或者编辑您的BaseEditTextStyle(如果您正在为EditText视图使用特定样式)。<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:importantForAutofill">noExcludeDescendants</item>
</style>
在values-v26/themes.xml
文件中,您可以简单地添加以下项目到应用程序中使用的默认主题中:editTextStyle
和android:editTextStyle
,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:editTextStyle">@style/App_EditTextStyle</item>
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>
这样,您就可以将此更改应用于 所有 EditText,而无需更改布局文件或活动(以后在修复错误时可以轻松删除它)。
这有可能吗?
我不知道。当然,没有任何文档可以证明。
是否有比这更好的方法?
我不知道有什么更好的方法。
<application>
。 - CommonsWareandroid:importantForAutofill="no"
。这应该是一个临时解决方案,仅适用于api 26+。创建自定义EditText样式,并将android:importantForAutofill
设置为no
。
<style name="EditTextStyleWithoutAutoFill" parent="Widget.AppCompat.EditText">
<item name="android:importantForAutofill">no</item>
</style>
然后在你的活动主题中为editTextStyle设置这个样式。
<item name="android:editTextStyle">@style/EditTextStyleWithoutAutoFill</item>
importantForAutofill
在这里有文档记录:https://developer.android.com/guide/topics/text/testautofill.html#trigger_autofill_in_your_app - SoftWyerres/values-v26/styles.xml
合并,以仅影响 Android 8.0,而不是 8.1(我无法相信 Google 发布了如此巨大的错误!) - Someone Somewhereandroid:importantForAutofill="no"
是无法解决我的问题的。<EditText
android:importantForAutofill="no"
tools:targetApi="o"
android:autofillHints="AUTOFILL_HINT_SMS_OTP" ...
android:autofillHints="AUTOFILL_HINT_SMS_OTP"
的原因是,如果你长按 EditText,它仍然会弹出自动填充。基本上,我告诉了字段的自动填充,它正在等待永远不会发送的短信。这有点像一个 hack,我知道...xmlns:tools="http://schemas.android.com/tools"
添加到您的模式中。 <com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
android:text="@={model.emailAdress}"/>