main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/mima"
/>
<CheckBox android:text="显示密码" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/xianshifou"/>
</LinearLayout>
java代码:
public class M extends Activity {
private EditText et;
private CheckBox cb;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.mima);
cb=(CheckBox)findViewById(R.id.xianshifou);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(cb.isChecked()){
//设置EditText的密码为可见的
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//设置密码为隐藏的
et.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}