频道栏目
IT货架 > > 正文
Android文本输入框(EditText)切换密码的显示与隐藏
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:android   c   style   class   blog   code   

 

 

 

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());
                }
 
            }
        });
    }
     
}

 

 

Android文本输入框(EditText)切换密码的显示与隐藏,布布扣,bubuko.com

Android文本输入框(EditText)切换密码的显示与隐藏

标签:android   c   style   class   blog   code   

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1