首页 > 代码库 > Android文本输入框(EditText)切换密码的显示与隐藏
Android文本输入框(EditText)切换密码的显示与隐藏
package cc.c; import android.app.Activity; import android.os.Bundle; import android.text.Selection; import android.text.Spannable; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; /** * Demo描述: * 文本输入框(EditText)切换密码的显示与隐藏 * * 参考资料: * 1 http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html * 2 http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html * 3 http://blog.csdn.net/dawanganban/article/details/23374187 * 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()); } } }); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。