Android动态显示或隐藏密码框中的密码(Android学习笔记)

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/msg" />

    <EditText
        android:id="@+id/pwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:text="" />

    <CheckBox
        android:id="@+id/showpwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/cktext" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    private EditText myPassword=null;
    private CheckBox myCheckBox=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.myPassword=(EditText)findViewById(R.id.pwd);
        this.myCheckBox=(CheckBox)findViewById(R.id.showpwd);
        this.myCheckBox.setOnClickListener(new OnClickListenerImpl());
    }
    
    private class OnClickListenerImpl implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(MainActivity.this.myCheckBox.isChecked()){
                MainActivity.this.myPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                MainActivity.this.myPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
        
    }

 

posted @ 2013-09-26 09:52  寂静之秋  阅读(425)  评论(0编辑  收藏  举报
哈尔滨八零网