3.22 打勾顯示輸入的密碼 --EditText與setTransformationMethod


實現目標:

實現原理:

為CheckBox添加一個監聽器事件;

實現的源碼:

package edu.cquptzx.showPassword;

 

import android.app.Activity;

import android.os.Bundle;

import android.text.method.HideReturnsTransformationMethod;

import android.text.method.PasswordTransformationMethod;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.EditText;

 

publicclass ShowPasswordActivity extends Activity {

    private EditText edittext;

    private CheckBox checkbox; 

    /** Called when the activity is first created. */

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        /*find the object by IDs .*/

       

        edittext = (EditText) findViewById(R.id.et);

        checkbox = (CheckBox) findViewById(R.id.cb);

       

        /* add a listener to the CheckBox */

        checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()

        {

           publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked)

           {

              if(checkbox.isChecked())

              {

                  /* show the password*/

                  edittext.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

              }

              else

              {

                  /* hide the password */

                 edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());   

              }         

           }      

        });

    }

}

相關知識:

 

 

 

 

 

最后實現效果:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM