您的位置:首页 > 移动开发 > Android开发

android学习之-输入框密码显示与隐藏

2011-12-20 15:37 330 查看
package sucre.android;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
/**
* 这个例子通过checkbox的选中与否来判断是否显示EditText中的内容
* 学习的知识点为
* setTransformationMethod
* HideReturnsTransformationMethod
* PasswordTransformationMethod
* @author qiaolei
*
*/
public class EX03_22 extends Activity {
private EditText mEditText;
private CheckBox mCheckBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText)findViewById(R.id.mEditText);
mCheckBox = (CheckBox)findViewById(R.id.mCheckBox);
mCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(mCheckBox.isChecked()){//显示密码为可见内容
mEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
mCheckBox.setText("隐藏密码");
}else{//隐藏密码为不可见内容
mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
mCheckBox.setText("显示密码");
}
}
});
}
}

xml内容见附件
本文出自 “博客即日起停止更新” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/746611
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: