android实现密码框右侧显示小眼睛

本文实例为大家分享了android实现密码框右侧显示小眼睛的具体代码,供大家参考,具体内容如下

android实现密码框右侧显示小眼睛_第1张图片

实现效果



 
  
 
    
 
      
 
      
    
 
    
      
 
        
 
        

//主要代码如下 


      
 
        
 
        
      
 

重要代码

private boolean isHideFirst = true;// 输入框密码是否是隐藏的,默认为true
 
  if (isHideFirst == true) {
            imageView.setImageResource(R.drawable.open);
            //密文
            HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
            et_password_code.setTransformationMethod(method1);
            isHideFirst = false;
          } else {
            imageView.setImageResource(R.drawable.close);
            //密文
            TransformationMethod method = PasswordTransformationMethod.getInstance();
            et_password_code.setTransformationMethod(method);
            isHideFirst = true;
 
          }
     // 光标的位置 
    int index = et_password_code.getText().toString().length();
    et_password_code.setSelection(index);
package com.ynrd.entrepreneur.activity;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.ynrd.entrepreneur.R;
import com.ynrd.entrepreneur.utils.CountDownTimerUtils;
 
public class RegisterActivity extends BaseCommonActivity implements View.OnClickListener {
 
  private Button bu_register_code;
  private TextView tv_register_login_password;
  private boolean isHideFirst = true;// 输入框密码是否是隐藏的,默认为true
  private boolean isFirst = true;// 是否密码登录,默认是验证码登录
 
  private ImageView imageView;
  private EditText et_password_code;
  private LinearLayout linear_password_code,password_liner;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
  }
 
  @Override
  protected void initViews() {
    super.initViews();
    //获取验证码
    bu_register_code = findViewById(R.id.bu_register_code);
    tv_register_login_password = findViewById(R.id.tv_register_login_password);
    imageView = findViewById(R.id.imageView);
    et_password_code = findViewById(R.id.et_password_code);
    linear_password_code = findViewById(R.id.linear_password_code);
    password_liner = findViewById(R.id.password_liner);
  }
 
  @Override
  protected void initListeners() {
    super.initListeners();
    bu_register_code.setOnClickListener(this);
    tv_register_login_password.setOnClickListener(this);
    imageView.setOnClickListener(this);
    imageView.setImageResource(R.drawable.close);
  }
 
  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.bu_register_code:
        //验证码方法 后期调用服务器
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(bu_register_code, 30000, 1000);
        mCountDownTimerUtils.start();
        break;
      case R.id.tv_register_login_password:
        if (isFirst == true){
          //切换成密码登录
          linear_password_code.setVisibility(View.GONE);
          password_liner.setVisibility(View.VISIBLE);
          tv_register_login_password.setText("使用短信验证码登录 >");
          isFirst = false;
        }else{
          //切换成验证码登录
          linear_password_code.setVisibility(View.VISIBLE);
          password_liner.setVisibility(View.GONE);
          tv_register_login_password.setText("使用密码登录 >");
          isFirst = true;
        }
        break;
      case R.id.imageView:
          if (isHideFirst == true) {
            imageView.setImageResource(R.drawable.open);
            //密文
            HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
            et_password_code.setTransformationMethod(method1);
            isHideFirst = false;
          } else {
            imageView.setImageResource(R.drawable.close);
            //密文
            TransformationMethod method = PasswordTransformationMethod.getInstance();
            et_password_code.setTransformationMethod(method);
            isHideFirst = true;
 
          }
          // 光标的位置 
          int index = et_password_code.getText().toString().length();
          et_password_code.setSelection(index);
        break;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(android实现密码框右侧显示小眼睛)