.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入:" />
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:textSize="30px" />
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码" />
</LinearLayout>
.java代码如下:
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class Hello extends Activity {
private EditText password = null;
private CheckBox show = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
this.password = (EditText) super.findViewById(R.id.txt1);
this.show = (CheckBox) super.findViewById(R.id.checkbox1);
this.show.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View arg0) {
if (show.isChecked()) {
Hello.this.password
.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
Hello.this.password
.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
}
}
运行如下: