实现输入框密码文本的显示与隐藏有两种,一种是通过直接改变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 | Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL . |
textCapCharacters |
0x00001001 | Can be combined with text and its variations to request capitalization of all characters. Corresponds to TYPE_TEXT_FLAG_CAP_CHARACTERS . |
textCapWords |
0x00002001 | Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS . |
textCapSentences |
0x00004001 | Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES . |
textAutoCorrect |
0x00008001 | Can be combined with text and its variations to request auto-correction of text being input. Corresponds to TYPE_TEXT_FLAG_AUTO_CORRECT . |