Android文本输入框(EditText)输入密码时显示与隐藏
内容摘要
代码很简单,这里就不多废话了。
http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html
* 2 http://developer.android.com/
http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html
* 2 http://developer.android.com/
文章正文
代码很简单,这里就不多废话了。
http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html
* 2 http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html
* Thank you very much
*/
public class MainActivity extends Activity {
private Button mSwitchButton;
private EditText mPasswordEditText;
private boolean isHidden=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mSwitchButton=(Button) findViewById(R.id.button);
mPasswordEditText=(EditText) findViewById(R.id.editText);
mSwitchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isHidden) {
//设置EditText文本为可见的
mPasswordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//设置EditText文本为隐藏的
mPasswordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
isHidden = !isHidden;
mPasswordEditText.postInvalidate();
//切换后将EditText光标置于末尾
CharSequence charSequence = mPasswordEditText.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
});
}
}
* 2 http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html
* Thank you very much
*/
public class MainActivity extends Activity {
private Button mSwitchButton;
private EditText mPasswordEditText;
private boolean isHidden=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mSwitchButton=(Button) findViewById(R.id.button);
mPasswordEditText=(EditText) findViewById(R.id.editText);
mSwitchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isHidden) {
//设置EditText文本为可见的
mPasswordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//设置EditText文本为隐藏的
mPasswordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
isHidden = !isHidden;
mPasswordEditText.postInvalidate();
//切换后将EditText光标置于末尾
CharSequence charSequence = mPasswordEditText.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
});
}
}
有相同需求的小伙伴过来参考下吧,可以直接拿去使用。
代码注释