在登录是出现的删除图标,登录密码时出现的小眼睛
账号的工具类:
//账号
public class CleanEditButton extends AppCompatImageView {
public CleanEditButton(Context context) {
super(context);
}
public CleanEditButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CleanEditButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//绑定EditText
public void bindEditText(EditText editText){
//editText 添加文字监听
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//如果输入文字就就显示 否则就隐藏
if (editText.getText().toString().length()>0){
setVisibility(VISIBLE);
}else {
setVisibility(GONE);
}
}
});
}
}
密码的工具类:
//密码
public class TogglePasswordButton extends AppCompatImageView {
private boolean isOpen=false;//默认眼睛是闭
private EditText mPassWord;
private OnClickListener mListener;
public TogglePasswordButton(Context context) {
super(context);
initClickListener();
}
public TogglePasswordButton(Context context, AttributeSet attrs) {
super(context, attrs);
initClickListener();
}
public TogglePasswordButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initClickListener();
}
private void initClickListener(){
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果外面进行了监听 就调用on Click
if (mListener!=null){
mListener.onClick(v);
}//如果没有就执行toggle方法
else {
toggle();
}
}
});
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
mListener = l;
}
private void toggle() {
if (isOpen){
setBackgroundResource(R.drawable.ic_eyes_close);
isOpen=false;
//PasswordTransformationMethod 密码变化成隐藏
mPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
}else {
setBackgroundResource(R.drawable.ic_eyes_open);
isOpen=true;
mPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
public void bindEditText(EditText editText){
mPassWord =editText;
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (editText.getText().toString().length()>0){
setVisibility(VISIBLE);
}else {
setVisibility(GONE);
}
}
});
}
}
网友评论