需求:
输入框一旦有值,即显示删除图标;点击切换明密文按钮,可以切换
布局:
代码
//监听密码是否输入login_pwd.addTextChangedListener(new TextWatcher() { private CharSequence temp; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { temp = s; } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (temp.length() > 0) { //限制长度 login_pwd_clean.setVisibility(View.VISIBLE); } else { login_pwd_clean.setVisibility(View.INVISIBLE); } }});
login_pwd_clean.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login_pwd.setText(""); login_pwd_clean.setVisibility(View.INVISIBLE); } });
//密码显示明文login_change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginActivity.this.login_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); login_change.setVisibility(View.GONE); login_change2.setVisibility(View.VISIBLE); }});//密码显示密文login_change2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginActivity.this.login_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); login_change2.setVisibility(View.GONE); login_change.setVisibility(View.VISIBLE); }});