1.布局文件很简单,一个EditText,一个Button。
<EditText
android:id="@+id/et_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/bn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/et_pwd"
android:layout_alignTop="@id/et_pwd"
android:text="SHOW" />
2.实现功能
public class MainActivity extends Activity {
private EditText etPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPwd = (EditText) findViewById(R.id.et_pwd);
findViewById(R.id.bn_show).setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
etPwd.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
break;
default:
etPwd.setTransformationMethod(PasswordTransformationMethod
.getInstance());
etPwd.postInvalidate();
break;
}
return false;
}
});
}
}