Android: How to set password property in an edit t

2019-01-22 11:28发布

问题:

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?

回答1:

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.



回答2:

This is deprecated

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

Edit

android:inputType="textPassword"


回答3:

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"



回答4:

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



回答5:

The only way that worked for me using code (not XML) is this one:

etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());


回答6:

See this link text view android:password

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



回答7:

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>


回答8:

To set password enabled in EditText, We will have to set an "inputType" attribute in xml file.If we are using only EditText then we will have set input type in EditText as given in below code.

                    <EditText
                    android:id="@+id/password_Edit"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="password"
                    android:imeOptions="actionNext"
                    android:inputType="textPassword"
                    android:maxLength="100"
                    android:nextFocusDown="@+id/next"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

Password enable attribute is

 android:inputType="textPassword"

But if we are implementing Password EditText with Material Design (With Design support library) then we will have write code as given bellow.

<android.support.design.widget.TextInputLayout
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/txtInput_currentPassword"
                android:layout_width="match_parent"
                app:passwordToggleEnabled="false"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password_Edit"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="@string/hint_currentpassword"
                    android:imeOptions="actionNext"
                    android:inputType="textPassword"
                    android:maxLength="100"
                    android:nextFocusDown="@+id/next"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </android.support.design.widget.TextInputLayout>

@Note: - In Android SDK 24 and above, "passwordToggleEnabled" is by default true. So if we have the customs handling of show/hide feature in the password EditText then we will have to set it false in code as given above in .

app:passwordToggleEnabled="true"

To add above line, we will have to add below line in root layout.

xmlns:app="http://schemas.android.com/apk/res-auto"


回答9:

My search for a similar solution for Visual Studio 2015/Xamarin lead me to this thread. While setting the EditText.InputType to Android.Text.InputTypes.TextVariationVisiblePassword properly hid the password during user entry, it did not 'hide' the password if it was visible before the EditText Layout was rendered (before the user submitted their password entry). In order to hide a visible password after the user submits their password and the EditText Layout is rendered, I used EditText.TransformationMethod = PasswordTransformationMethod.Instance as suggested by LuxuryMode.