Android 数字密码字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2420181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:48:28  来源:igfitidea点击:

Android numeric password field

androidandroid-edittext

提问by Viktor Bre?an

I have an EditText field which needs to be a numeric password field. Everything works OK in portrait mode; not in landscape. When the user selects the EditText field, the UI zooms into the field and when I type all the characters are visible.

我有一个 EditText 字段,它需要是一个数字密码字段。在纵向模式下一切正常;不在风景中。当用户选择 EditText 字段时,UI 会放大该字段,并且当我键入时,所有字符都可见。

I need a numeric keyboard also. I tried setting the input type to text password|number. If I remove "number" option everything works right; otherwise no.

我也需要一个数字键盘。我尝试将输入类型设置为文本密码|数字。如果我删除“数字”选项,一切正常;否则没有。

Any suggestions?

有什么建议?

回答by Viktor Bre?an

This problem can be solved without using deprecated android:password. Use the following code snippet, but do not reverse the sequence of calls:

这个问题可以在不使用 deprecated 的情况下解决android:password。使用以下代码片段,但不要颠倒调用顺序:

    EditText editText = (EditText) findViewById(R.id.MyEditText);
    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());

回答by robnick

The simplest solution that I found for a numeric text entry that needed to be hidden (password style) was to use: android:inputType="numberPassword"

我为需要隐藏的数字文本条目(密码样式)找到的最简单的解决方案是使用: android:inputType="numberPassword"

Full example that worked for me:

对我有用的完整示例:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/MyLargeTextBox"
    android:background="@android:drawable/editbox_background_normal"
    android:id="@+id/txtWebServicePIN"
    android:layout_row="3"
    android:layout_column="2"
    android:layout_columnSpan="2"
    android:minWidth="100dp"
    android:maxWidth="100dp"
    android:inputType="numberPassword"
    android:maxLength="6"/>

回答by Adam L.

To fix the issue where the password is visible in the landscape mode full-screen editor (as Marcus described), you can add the following line to disable the full-screen editor:

要解决密码在横向模式全屏编辑器中可见的问题(如 Marcus 所述),您可以添加以下行以禁用全屏编辑器:

editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

You may also want to change the typeface to monospace to better emulate the text-password behavior:

您可能还想将字体更改为等宽字体以更好地模拟文本密码行为:

editText.setTypeface(Typeface.MONOSPACE);

回答by Diego Torres Milano

Using the deprecated

使用已弃用的

android:password="true"

may solve your problem.

可能会解决您的问题。

回答by pradip

Try this in XML & nothing changes in manifest file,

在 XML 中尝试此操作,清单文件中没有任何更改,

<EditText
         android:id="@+id/etPassword"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:inputType="textPassword"
         android:singleLine="true" />

回答by Bhimbim

This how to make input password that has hint which not converted to * !!.

这如何使输入密码具有未转换为 * 的提示 !!。

On XML :

在 XML 上:

android:inputType="textPassword"
android:gravity="center"
android:ellipsize="start"
android:hint="Input Password !."

thanks to : mango and rjrjr for the insight :D.

感谢:mango 和 rjrjr 的洞察力:D。

回答by atiruz

Try with this:

试试这个:

EditText input = (EditText) findViewById(R.id.input);
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

More info HERE

更多信息在这里

回答by MrSnowflake

I think you should use android:password="true"and android:numeric="integer". But as the Android documentation states (in R.attr.html, not in the TextViewdocs), these are deprectated and you should use android:inputType. inputType takes flag, so you can mix mulitple possibilities. And I think android:inputType="number | password"should work for you.

我认为你应该使用android:password="true"android:numeric="integer"。但正如 Android 文档所述(在R.attr.html,而不是在TextView文档中),这些已弃用,您应该使用android:inputType. inputType 带有标志,因此您可以混合多种可能性。我认为android:inputType="number | password"应该为你工作。

You should specify android:password="true"if you want to support older devices too.

您应该指定android:password="true"是否也想支持旧设备。