实现输入框密码文本的显示与隐藏有两种,一种是通过直接改变android:inputType,一种是通过改变android.text.method.TransformationMethod。

方式一:改变android:inputType的值

实现代码如下:

/**

* 密码显示或隐藏 (切换)

*/

private void showOrHide(EditText etPassword){

//记住光标开始的位置

int pos = etPassword.getSelectionStart();

if(etPassword.getInputType()!= (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)){//隐藏密码

etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

}else{//显示密码

etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

}

etPassword.setSelection(pos);

}

对应的值与描述如下:

Constant

Value

Description

none

0x00000000

There is no content type. The text is not editable.

text

0x00000001

textCapCharacters

0x00001001

Can be combined with text and its variations to request capitalization of all characters. Corresponds to

textCapWords

0x00002001

Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to

textCapSentences

0x00004001

Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to

textAutoCorrect

0x00008001

Can be combined with text and its variations to request auto-correction of text being input. Corresponds to

textAutoComplete

0x00010001

Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds to

textMultiLine

0x00020001

Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to

textImeMultiLine

0x00040001

Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds to

textNoSuggestions

0x00080001

Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to

textUri

0x00000011

Text that will be used as a URI. Corresponds to

textEmailAddress

0x00000021

Text that will be used as an e-mail address. Corresponds to

textEmailSubject

0x00000031

Text that is being supplied as the subject of an e-mail. Corresponds to

textShortMessage

0x00000041

Text that is the content of a short message. Corresponds to

textLongMessage

0x00000051

Text that is the content of a long message. Corresponds to

textPersonName

0x00000061

Text that is the name of a person. Corresponds to

textPostalAddress

0x00000071

Text that is being supplied as a postal mailing address. Corresponds to

textPassword

0x00000081

textVisiblePassword

0x00000091

Text that is a password that should be visible. Corresponds to

textWebEditText

0x000000a1

Text that is being supplied as text in a web form. Corresponds to

textFilter

0x000000b1

Text that is filtering some other data. Corresponds to

textPhonetic

0x000000c1

Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds to

textWebEmailAddress

0x000000d1

Text that will be used as an e-mail address on a web form. Corresponds to

textWebPassword

0x000000e1

Text that will be used as a password on a web form. Corresponds to

number

0x00000002

numberSigned

0x00001002

Can be combined with number and its other options to allow a signed number. Corresponds to

numberDecimal

0x00002002

Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds to

numberPassword

0x00000012

phone

0x00000003

For entering a phone number. Corresponds to

datetime

0x00000004

date

0x00000014

time

0x00000024

This corresponds to the global attribute resource symbol

方式二:改变android.text.method.TransformationMethod 的值。

实现代码如下:

/**

* 显示或隐藏

* @param isShow

*/

private void showOrHide(boolean isShow){

//记住光标开始的位置

int pos = etPassword.getSelectionStart();

if(isShow){

etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

}else{

etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

}

etPassword.setSelection(pos);

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐