android:设置密码隐藏与显示
main.xml:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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"
/>
<CheckBoxandroid:text="显示密码" android:layout_width="wrap_content"
android:layout_height="wrap_content"android:id="@+id/xianshifou"/>
</LinearLayout>
java代码:
public class M extends Activity {
privateEditText et;
privateCheckBox cb;
public voidonCreate(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());
}
}
});
}
}
作者:get123
补充:移动开发 , Android ,