Android Cleanedittext automatically with X number input box EditText

Source: Internet
Author: User


Icon is here. You know
The effect of the implementation is as follows:
The problem of doing the project today. Recorded. EditText with the Delete button automatically.
  
 
  1. public class CleanEditText extends EditText implements OnFocusChangeListener, TextWatcher {
  2. /**
  3. * 删除按钮的引用
  4. */
  5. private Drawable mClearDrawable;
  6. public CleanEditText(Context context) {
  7. this(context, null);
  8. }
  9. public CleanEditText(Context context, AttributeSet attrs) {
  10. //这里构造方法也很重要,不加这个很多属性不能再XML里面定义
  11. this(context, attrs, android.R.attr.editTextStyle);
  12. }
  13. public CleanEditText(Context context, AttributeSet attrs, int defStyle) {
  14. super(context, attrs, defStyle);
  15. init();
  16. }
  17. private void init() {
  18. //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
  19. mClearDrawable = getCompoundDrawables()[2];
  20. if (mClearDrawable == null) {
  21. mClearDrawable = getResources()
  22. .getDrawable(R.drawable.ic_clear_black);
  23. }
  24. mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
  25. setClearIconVisible(false);
  26. setOnFocusChangeListener(this);
  27. addTextChangedListener(this);
  28. }
  29. /**
  30. * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
  31. * 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和
  32. * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
  33. */
  34. @Override
  35. public boolean onTouchEvent(MotionEvent event) {
  36. if (getCompoundDrawables()[2] != null) {
  37. if (event.getAction() == MotionEvent.ACTION_UP) {
  38. boolean touchable = event.getX() > (getWidth()
  39. - getPaddingRight() - mClearDrawable.getIntrinsicWidth())
  40. && (event.getX() < ((getWidth() - getPaddingRight())));
  41. if (touchable) {
  42. this.setText("");
  43. }
  44. }
  45. }
  46. return super.onTouchEvent(event);
  47. }
  48. /**
  49. * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
  50. */
  51. @Override
  52. public void onFocusChange(View v, boolean hasFocus) {
  53. if (hasFocus) {
  54. setClearIconVisible(getText().length() > 0);
  55. } else {
  56. setClearIconVisible(false);
  57. }
  58. }
  59. /**
  60. * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
  61. * @param visible
  62. */
  63. protected void setClearIconVisible(boolean visible) {
  64. if (this.isEnabled()) {
  65. Drawable right = visible ? mClearDrawable : null;
  66. setCompoundDrawables(getCompoundDrawables()[0],
  67. getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
  68. }
  69. }
  70. /**
  71. * 当输入框里面内容发生变化的时候回调的方法
  72. */
  73. @Override
  74. public void onTextChanged(CharSequence s, int start, int count, int after) {
  75. setClearIconVisible(s.length() > 0);
  76. }
  77. @Override
  78. public void beforeTextChanged(CharSequence s, int start, int count,
  79. int after) {
  80. }
  81. @Override
  82. public void afterTextChanged(Editable s) {
  83. }
  84. /**
  85. * 设置晃动动画
  86. */
  87. public void setShakeAnimation(){
  88. this.setAnimation(shakeAnimation(5));
  89. }
  90. /**
  91. * 晃动动画
  92. * @param counts 1秒钟晃动多少下
  93. * @return
  94. */
  95. public static Animation shakeAnimation(int counts){
  96. Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
  97. translateAnimation.setInterpolator(new CycleInterpolator(counts));
  98. translateAnimation.setDuration(1000);
  99. return translateAnimation;
  100. }
  101. }



From for notes (Wiz)

List of attachments
    • Ic_clear_black.png
    • Ic_clear_black_2.png

EditText of Cleanedittext automatically with X entry box in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.