- 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("显示密码");
- }
- }
- });
- }
- }
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/746611,如需转载请自行联系原作者