2424import android .animation .ValueAnimator ;
2525import android .animation .ValueAnimator .AnimatorUpdateListener ;
2626import android .text .Editable ;
27- import android .text .TextWatcher ;
2827import android .view .View ;
2928import android .view .View .OnClickListener ;
3029import android .view .View .OnFocusChangeListener ;
3130import android .widget .EditText ;
3231import androidx .annotation .DrawableRes ;
3332import androidx .annotation .NonNull ;
33+ import androidx .annotation .Nullable ;
3434import com .google .android .material .animation .AnimationUtils ;
35- import com .google .android .material .textfield .TextInputLayout .OnEditTextAttachedListener ;
3635import com .google .android .material .textfield .TextInputLayout .OnEndIconChangedListener ;
3736
3837/** Default initialization of the clear text end icon {@link TextInputLayout.EndIconMode}. */
@@ -42,42 +41,6 @@ class ClearTextEndIconDelegate extends EndIconDelegate {
4241 private static final int ANIMATION_SCALE_DURATION = 150 ;
4342 private static final float ANIMATION_SCALE_FROM_VALUE = 0.8f ;
4443
45- private final TextWatcher clearTextEndIconTextWatcher =
46- new TextWatcher () {
47- @ Override
48- public void beforeTextChanged (CharSequence s , int start , int count , int after ) {}
49-
50- @ Override
51- public void onTextChanged (CharSequence s , int start , int before , int count ) {}
52-
53- @ Override
54- public void afterTextChanged (@ NonNull Editable s ) {
55- if (endLayout .getSuffixText () != null ) {
56- return ;
57- }
58- animateIcon (shouldBeVisible ());
59- }
60- };
61- private final OnFocusChangeListener onFocusChangeListener =
62- new OnFocusChangeListener () {
63- @ Override
64- public void onFocusChange (View v , boolean hasFocus ) {
65- animateIcon (shouldBeVisible ());
66- }
67- };
68- private final OnEditTextAttachedListener clearTextOnEditTextAttachedListener =
69- new OnEditTextAttachedListener () {
70- @ Override
71- public void onEditTextAttached (@ NonNull TextInputLayout textInputLayout ) {
72- EditText editText = textInputLayout .getEditText ();
73- textInputLayout .setEndIconVisible (shouldBeVisible ());
74- editText .setOnFocusChangeListener (onFocusChangeListener );
75- endIconView .setOnFocusChangeListener (onFocusChangeListener );
76- // Make sure there's always only one clear text text watcher added
77- editText .removeTextChangedListener (clearTextEndIconTextWatcher );
78- editText .addTextChangedListener (clearTextEndIconTextWatcher );
79- }
80- };
8144 private final OnEndIconChangedListener endIconChangedListener =
8245 new OnEndIconChangedListener () {
8346 @ Override
@@ -89,21 +52,33 @@ public void onEndIconChanged(@NonNull TextInputLayout textInputLayout, int previ
8952 new Runnable () {
9053 @ Override
9154 public void run () {
92- editText .removeTextChangedListener (clearTextEndIconTextWatcher );
9355 // Make sure icon view is visible.
9456 animateIcon (/* show= */ true );
9557 }
9658 });
97- if (editText .getOnFocusChangeListener () == onFocusChangeListener ) {
98- editText .setOnFocusChangeListener (null );
99- }
100- if (endIconView .getOnFocusChangeListener () == onFocusChangeListener ) {
101- endIconView .setOnFocusChangeListener (null );
102- }
10359 }
10460 }
10561 };
10662
63+ private final OnClickListener onIconClickListener = new OnClickListener () {
64+ @ Override
65+ public void onClick (View v ) {
66+ Editable text = textInputLayout .getEditText ().getText ();
67+ if (text != null ) {
68+ text .clear ();
69+ }
70+
71+ endLayout .refreshEndIconDrawableState ();
72+ }
73+ };
74+
75+ private final OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener () {
76+ @ Override
77+ public void onFocusChange (View v , boolean hasFocus ) {
78+ animateIcon (shouldBeVisible ());
79+ }
80+ };
81+
10782 private AnimatorSet iconInAnim ;
10883 private ValueAnimator iconOutAnim ;
10984
@@ -118,19 +93,6 @@ void initialize() {
11893 endLayout .setEndIconContentDescription (
11994 endLayout .getResources ().getText (R .string .clear_text_end_icon_content_description ));
12095 endLayout .setEndIconCheckable (false );
121- endLayout .setEndIconOnClickListener (
122- new OnClickListener () {
123- @ Override
124- public void onClick (View v ) {
125- Editable text = textInputLayout .getEditText ().getText ();
126- if (text != null ) {
127- text .clear ();
128- }
129-
130- endLayout .refreshEndIconDrawableState ();
131- }
132- });
133- textInputLayout .addOnEditTextAttachedListener (clearTextOnEditTextAttachedListener );
13496 endLayout .addOnEndIconChangedListener (endIconChangedListener );
13597 initAnimators ();
13698 }
@@ -143,6 +105,34 @@ void onSuffixVisibilityChanged(boolean visible) {
143105 animateIcon (visible );
144106 }
145107
108+ @ Override
109+ OnClickListener getOnIconClickListener () {
110+ return onIconClickListener ;
111+ }
112+
113+ @ Override
114+ public void onEditTextAttached (@ Nullable EditText editText ) {
115+ textInputLayout .setEndIconVisible (shouldBeVisible ());
116+ }
117+
118+ @ Override
119+ void afterEditTextChanged (@ NonNull Editable s ) {
120+ if (endLayout .getSuffixText () != null ) {
121+ return ;
122+ }
123+ animateIcon (shouldBeVisible ());
124+ }
125+
126+ @ Override
127+ OnFocusChangeListener getOnEditTextFocusChangeListener () {
128+ return onFocusChangeListener ;
129+ }
130+
131+ @ Override
132+ OnFocusChangeListener getOnIconViewFocusChangeListener () {
133+ return onFocusChangeListener ;
134+ }
135+
146136 private void animateIcon (boolean show ) {
147137 boolean shouldSkipAnimation = endLayout .isEndIconVisible () == show ;
148138 if (show && !iconInAnim .isRunning ()) {
0 commit comments