android:設置密碼隱藏與顯示

 main.xml:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="https://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

發佈留言