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]);
}
}
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]);
}
}
相关文章推荐
- android提供的算法工具类
- 【Android】ViewPager左右滑动和小圆点使用
- Android Studio 快捷键使用
- Android简单的Toast工具类
- Android Studio中如何打JAR包
- Activity转场动画(5.0+)
- Android打开系统相机、相册,并进行裁剪图片的工具类CameraUtil,以及相册获取图片注意要点。
- android 读取SD卡中MP3文件的信息
- Android中Service白色保活
- Android 利用ListView制作带竖线的多彩表格
- ①Android开发小技巧之ToolBar-文字居中
- Android 基础-------自定义控件
- armeabi-v7a armeabi arm64-v8a
- Android利用TrafficStats类实现监测手机网速功能,以及double数据小数点后保留2位
- Android开发从入门到放弃(2)保存Activity的状态
- Android软键盘(二) 软键盘弹出布局上移的问题
- Android studio 快捷键大全
- Android service启动流程分析.
- android sdk manager 更新失败
- Android 表情面板切换键盘闪烁问题的解决