Android numeric password field

2019-01-13 13:06发布

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.

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?

8条回答
Animai°情兽
2楼-- · 2019-01-13 13:16

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

    EditText editText = (EditText) findViewById(R.id.MyEditText);
    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-13 13:16

Try this in XML & nothing changes in manifest file,

<EditText
         android:id="@+id/etPassword"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:inputType="textPassword"
         android:singleLine="true" />
查看更多
可以哭但决不认输i
4楼-- · 2019-01-13 13:21

The simplest solution that I found for a numeric text entry that needed to be hidden (password style) was to use: 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"/>
查看更多
甜甜的少女心
5楼-- · 2019-01-13 13:21

Try with this:

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

More info HERE

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-13 13:25

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 TextView docs), 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.

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

查看更多
冷血范
7楼-- · 2019-01-13 13:39

Using the deprecated

android:password="true"

may solve your problem.

查看更多
登录 后发表回答