Android: How to set password property in an edit t

2019-01-22 10:48发布

I need to create a login form with 'username' 'password' fields and two buttons 'login' and 'cancel' in my android application.

I am using an alert dialog with edittext inside that.

This is the code I used to create password edittext..

     final EditText Password = new EditText(this);
     Password.setGravity(Gravity.CENTER);
     Password.setHint("Password");
     Password.setWidth(200);

     Password.setTransformationMethod(new PasswordTransformationMethod());
     login_alert.addView(Password);

My issue is that, plain text is shown instead of 'dots' when i open a softkeypad to edit the password. (It is shown as dots when not in softkeypad mode)

Can anyone suggest a solution?

9条回答
戒情不戒烟
2楼-- · 2019-01-22 11:22
Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

This one works for me.
But you have to look at Octavian Damiean's comment, he's right.

查看更多
再贱就再见
3楼-- · 2019-01-22 11:30

This is deprecated

In xml of EditText iclude this attribute: android:password="true"

Edit

android:inputType="textPassword"
查看更多
Melony?
4楼-- · 2019-01-22 11:30

You need to use PasswordTransformationMethod.getInstance() instead of new PasswordTransformationMethod().

查看更多
淡お忘
5楼-- · 2019-01-22 11:31

Here's a new way of putting dots in password

<EditText
    android:id="@+id/loginPassword"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="@string/pwprompt" /

add android:inputType = "textPassword"

查看更多
Bombasti
6楼-- · 2019-01-22 11:36

I found when doing this that in order to set the gravity to center, and still have your password hint show when using inputType, the android:gravity="Center" must be at the end of your XML line.

<EditText android:textColor="#000000" android:id="@+id/editText2" 
    android:layout_width="fill_parent" android:hint="Password" 
    android:background="@drawable/rounded_corner" 
    android:layout_height="fill_parent" 
    android:nextFocusDown="@+id/imageButton1" 
    android:nextFocusRight="@+id/imageButton1" 
    android:nextFocusLeft="@+id/editText1"
    android:nextFocusUp="@+id/editText1" 
    android:inputType="textVisiblePassword" 
    android:textColorHint="#999999" 
    android:textSize="16dp" 
    android:gravity="center">
</EditText>
查看更多
Fickle 薄情
7楼-- · 2019-01-22 11:42

See this link text view android:password

This applies for EditText as well, as it is a known direct subclass of TextView.

查看更多
登录 后发表回答