You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止软键盘保存密码EditText的输入内容?

阻止密码EditText被软键盘保存为新词的解决方案
  • XML布局直接配置
    在密码EditText的标签中添加以下两个属性:
    android:inputType="textPassword|textNoSuggestions"
    android:importantForAutofill="no"
    其中textNoSuggestions会告知输入法不要提供输入联想或保存该输入内容,importantForAutofill则直接禁用自动填充相关行为,双重避免密码被软键盘记录。

  • 代码中动态设置
    如果需要在代码里调整配置,可使用以下代码:
    Java 版本:

EditText passwordEt = findViewById(R.id.password_edittext);
passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
passwordEt.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);

Kotlin 版本:

val passwordEt = findViewById<EditText>(R.id.password_edittext)
passwordEt.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
passwordEt.importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO

注:部分第三方输入法对textNoSuggestions的支持可能存在差异,搭配importantForAutofill="no"能进一步提升效果。

内容的提问来源于stack exchange,提问作者Feuer und Wasser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 08:52:31