ca6ef9c190e05bc5678a03251aed502c60e62b0d.svn-base 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.hjq.widget;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.graphics.drawable.Drawable;
  5. import android.os.Build;
  6. import android.support.annotation.Nullable;
  7. import android.support.annotation.RequiresApi;
  8. import android.support.v4.content.ContextCompat;
  9. import android.support.v4.graphics.drawable.DrawableCompat;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.AttributeSet;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15. import android.widget.EditText;
  16. /**
  17. * desc : 带清除按钮的EditText
  18. */
  19. @SuppressLint("AppCompatCustomView")
  20. public final class ClearEditText extends EditText
  21. implements View.OnTouchListener, View.OnFocusChangeListener, TextWatcher {
  22. private Drawable mClearIcon;
  23. private OnTouchListener mOnTouchListener;
  24. private OnFocusChangeListener mOnFocusChangeListener;
  25. public ClearEditText(Context context) {
  26. super(context, null);
  27. }
  28. public ClearEditText(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. initialize(context);
  31. }
  32. public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
  33. super(context, attrs, defStyleAttr);
  34. initialize(context);
  35. }
  36. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  37. public ClearEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  38. super(context, attrs, defStyleAttr, defStyleRes);
  39. initialize(context);
  40. }
  41. private void initialize(final Context context) {
  42. final Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.widget_input_delete);
  43. final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); //Wrap the drawable so that it can be tinted pre Lollipop
  44. //DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
  45. mClearIcon = wrappedDrawable;
  46. mClearIcon.setBounds(0, 0, mClearIcon.getIntrinsicWidth(), mClearIcon.getIntrinsicHeight());
  47. setClearIconVisible(false);
  48. super.setOnTouchListener(this);
  49. super.setOnFocusChangeListener(this);
  50. super.addTextChangedListener(this);
  51. }
  52. private void setClearIconVisible(final boolean visible) {
  53. if (mClearIcon.isVisible() == visible) return;
  54. mClearIcon.setVisible(visible, false);
  55. final Drawable[] compoundDrawables = getCompoundDrawables();
  56. setCompoundDrawables(
  57. compoundDrawables[0],
  58. compoundDrawables[1],
  59. visible ? mClearIcon : null,
  60. compoundDrawables[3]);
  61. }
  62. @Override
  63. public void setOnFocusChangeListener(final OnFocusChangeListener onFocusChangeListener) {
  64. mOnFocusChangeListener = onFocusChangeListener;
  65. }
  66. @Override
  67. public void setOnTouchListener(final OnTouchListener onTouchListener) {
  68. mOnTouchListener = onTouchListener;
  69. }
  70. /**
  71. * {@link View.OnFocusChangeListener}
  72. */
  73. @Override
  74. public void onFocusChange(final View view, final boolean hasFocus) {
  75. if (hasFocus && getText() != null) {
  76. setClearIconVisible(getText().length() > 0);
  77. } else {
  78. setClearIconVisible(false);
  79. }
  80. if (mOnFocusChangeListener != null) {
  81. mOnFocusChangeListener.onFocusChange(view, hasFocus);
  82. }
  83. }
  84. /**
  85. * {@link View.OnTouchListener}
  86. */
  87. @Override
  88. public boolean onTouch(final View view, final MotionEvent motionEvent) {
  89. final int x = (int) motionEvent.getX();
  90. if (mClearIcon.isVisible() && x > getWidth() - getPaddingRight() - mClearIcon.getIntrinsicWidth()) {
  91. if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
  92. setText("");
  93. }
  94. return true;
  95. }
  96. return mOnTouchListener != null && mOnTouchListener.onTouch(view, motionEvent);
  97. }
  98. /**
  99. * {@link TextWatcher}
  100. */
  101. @Override
  102. public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
  103. if (isFocused()) {
  104. setClearIconVisible(s.length() > 0);
  105. }
  106. }
  107. @Override
  108. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  109. @Override
  110. public void afterTextChanged(Editable s) {}
  111. // @Override
  112. // protected void drawableStateChanged() {
  113. // super.drawableStateChanged();
  114. // }
  115. }