Android显示和隐藏密码的自定义EditView
来源:互联网 发布:程序员图片 编辑:程序博客网 时间:2024/06/06 09:57
自定一个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]); }}
0 0
- Android显示和隐藏密码的自定义EditView
- EditView密码隐藏显示
- EditView密码的显示与隐藏
- android 关于软键盘的显示和隐藏监听,解决挡住Editview的问题
- android 实现密码的显示和隐藏
- Android 实现显示密码和隐藏密码
- android 根据checkBox动态控制密码的显示和隐藏
- Android EditText控制密码的显示和隐藏
- Android基础--登陆界面,密码的隐藏和显示
- Android EditText密码显示隐藏和全部属性的介绍
- Android EditText密码显示隐藏和全部属性的介绍
- 写写Android 登陆框密码的显示和隐藏
- android开发隐藏和显示密码
- Android:密码显示隐藏
- Android:密码显示隐藏
- android 显示隐藏密码
- Android显示隐藏密码
- Android:密码显示隐藏
- linear-gradient用法
- Spring+SpringMVC+MyBatis整合实例
- 被风吹过的夏天,与往事和解。
- Atitit ABI FFI 的区别与联系 attilax总结
- Atitit ABI FFI 的区别与联系 attilax总结
- Android显示和隐藏密码的自定义EditView
- Main() 方法的参数
- Josephus ring
- 46. Permutations
- 数据库时间(datetime类型)的存储和读取
- 2016滴滴出行研发工程师笔试题(亮灯问题)
- android提供的算法工具类
- GO语言学习笔记三
- PHP的strcmp函数引发的安全问题