Android: How to set password property in an edit text?

前端 未结 9 2038
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 05:59

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 w

相关标签:
9条回答
  • 2020-12-14 06:09

    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"
    
    0 讨论(0)
  • 2020-12-14 06:10
    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.

    0 讨论(0)
  • 2020-12-14 06:12

    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());
    
    0 讨论(0)
  • 2020-12-14 06:14

    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>
    
    0 讨论(0)
  • 2020-12-14 06:18

    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"

    0 讨论(0)
  • 2020-12-14 06:29

    See this link text view android:password

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

    0 讨论(0)
提交回复
热议问题