android開發中將EditText輸入的明文與密文之間的切換
阿新 • • 發佈:2018-12-22
其實很簡單,獲取輸入的之後,設定一個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; importandroid.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(); } }); } }