您的位置:首页 > 移动开发 > Android开发

Android显示和隐藏密码的自定义EditView

2016-12-22 00:12 621 查看
自定一个EditView,设置其DrawableRight为显示或隐藏密码属性,同时当点击drawable时,阻止弹出输入键盘功能。

public class PasswordEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {

private Drawable mDrawable;
private boolean hasFocus;

public PasswordEditText(Context context) {
this(context, null);
}

public PasswordEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}

public PasswordEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}

private void initView() {
//get the DrawableRight,
mDrawable = getCompoundDrawables()[2]; //drawables position: left top right bottom.
if (mDrawable == null) {
mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.invisible); //the visible and invisible icon
}
mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
//set icon visible in default view
setIconVisible(true);
//set focus listener
setOnFocusChangeListener(this);
//add text change listener
addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {

}

@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFocus) {
setIconVisible(true);
}
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
this.hasFocus = hasFocus;
if (hasFocus) {
setIconVisible(true);
} else {
setIconVisible(false);
}

}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int x = (int) event.getX();
int y = (int) event.getY();
Rect rect = getCompoundDrawables()[2].getBounds();
int height = rect.height();
int distance = (getHeight() - height) / 2;
boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
boolean isInnerHeight = y > distance && y < (distance + height);
if (isInnerWidth && isInnerHeight) {
if (this.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
this.setTransformationMethod(PasswordTransformationMethod.getInstance());
mDrawable = getCompoundDrawables()[2]; //the right drawable
mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.invisible);
mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
setIconVisible(true);
return true; //stop the event
} else {
this.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
mDrawable = getCompoundDrawables()[2]; //the right drawable
mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.visible);
mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
setIconVisible(true);
return true; //stop the event
}
}
}
}
return super.onTouchEvent(event);
}

protected void setIconVisible(boolean visible) {
Drawable right = visible ? mDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息