1. 程式人生 > >android開發中將EditText輸入的明文與密文之間的切換

android開發中將EditText輸入的明文與密文之間的切換

其實很簡單,獲取輸入的之後,設定一個showPassword布林值,然後進行判斷。
       很簡單,程式碼如下:
package com.kuaishouyin.activity;
/**
 * 註冊頁面
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import 
android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; import com.kuaishouyin.R; public class RegistActivity extends Activity { private EditText etPwd; private ImageView ivYanjing
; private String etpwd; private Boolean showPassword = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_regist); etPwd = (EditText) findViewById(R.id.regist_et_pwd); ivYanjing
= (ImageView) findViewById(R.id.regist_iv_yanjing);
        //獲取輸入的值
           etpwd = etPwd.getText().toString().trim();
public void onClick(View view) { if (showPassword) { etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); } showPassword = !showPassword; etPwd.postInvalidate(); } }); } }