Android password box, EditText

Table of contents

1. Basic usage

2. Display and hiding of passwords

(1) Use setTransformationMethod method

(2) Use the setInputType method

3. imeOptions attribute

4. Single line settings

5. Focus position


When using the password box in Android, EditText is generally used to set inputType="textPassword" .

1. Basic usage

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码" 

    android:inputType="textPassword" 

    android:maxLines="1"

    android:imeOperation="actionDone" 或  android:imeOperation="actionNext"
    />

2. Display and hiding of passwords

There are two methods for displaying and hiding passwords, the setTransformationMethod (setting change method) method and the setInputType method ; please note that if the android:hint attribute (i.e. prompt) is set, using the setInputType method will cause the font of the hint to change . It is recommended to use the setTransformationMethod method. .

(1) Use setTransformationMethod method

// 密码可见
passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
 
// 密码隐藏
passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());

(2) Use the setInputType method

// 密码可见
passwordET.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
 
// 密码隐藏
passwordET.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);

3. imeOptions attribute

imeOptions is an attribute of EditText in Android, used to specify the behavior of the soft keyboard . It controls the displayed text of the "Enter" or "Done" button in the lower right corner of the keyboard and the behavior when the button is pressed. Please note that the inputType attribute must be set when setting this attribute, otherwise it will be invalid.

Attribute values ​​can be:

actionDone : Display the "Done" button. Suitable for single-line input, indicating that the user input is completed.

actionGo : Display the "Go" button. Applicable to search boxes, indicating that the user has finished typing and wishes to perform a search operation.

actionSearch : Display the "Search" button. Applicable to search boxes, indicating that the user has finished typing and wishes to perform a search operation. 

actionSend : Display the "Send" button. Suitable for message input boxes, indicating that the user has finished inputting and wants to send a message.

actionNext : Display the "Next" button. Suitable for multi-line input, indicating that the user wants to move to the next input box.

4. Single line settings

To avoid multiple lines, single-line settings can be made.

It is recommended to use it with the imeOperation attribute to replace the Enter key! ! !

//最大行数(推荐)
android:maxLines="1"

//单行(已淘汰)
android:singleLine="true"

5. Focus position

If you want the focus to automatically appear at the end of the string , you can set a focus change listener and use the .setSelection( int index ) method to set the focus position when the edit box gets focus.

//工具类
public class EditTextUtils {
    /**
     * 设置EditText焦点出现在末尾
     * @param editTexts N个EditText
     */
    public static void setFocusAtLast(EditText ...editTexts){
        for(int i=0;i<editTexts.length;i++){
            EditText editText=editTexts[i];
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View view, boolean b) {
                    if(b){
                        EditText editText=(EditText) view;
                        editText.setSelection(editText.getText().length());
                    }
                }
            });
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_57150356/article/details/134695168