zoukankan      html  css  js  c++  java
  • 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());
                    }
     
                }
            });
        }
         
    }
  • 相关阅读:
    svn安装教程
    六、数组类的创建
    五、顺序存储线性表分析
    四、StaticList 和 DynamicList
    三、顺序存储结构的抽象实现
    二、线性表的顺序存储结构
    一、线性表的本质和操作
    专题五:局部变量、全局变量global、导入模块变量
    专题四:文件基础知识、字符编码
    专题3-2:列表基础知识:二维list排序、获取下标和处理txt文本实例
  • 原文地址:https://www.cnblogs.com/taoweiji/p/3766627.html
Copyright © 2011-2022 走看看