一个关于password 的细节,就是在登陆的时候,如果用户名、密码都OK,我们点击提交或者登陆就万事大吉了!但有时候我们会遇到“你的用户名或者密码有误”的情况,相当郁闷啊,要重新输入一次。如果输入的密码“可见”,可以少做这种无用功,用户体验也会好些!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="@string/userName" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/cbShowPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPassword"
android:layout_below="@+id/etPassword"
android:layout_marginTop="20dp"
android:text="@string/show_pwd" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="22dp"
android:layout_marginTop="20dp"
android:text="@string/pwd"
tools:context=".MainActivity" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPassword"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:ems="10" />
</RelativeLayout>
public class MainActivity extends Activity {
private EditText pwd;
private CheckBox checkPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pwd=(EditText) findViewById(R.id.etPassword);
checkPwd=(CheckBox) findViewById(R.id.cbShowPwd);
checkPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (!isChecked) {
// show password
pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
}
效果图示: