Skip to content

Commit

Permalink
Fix OnTextClearedListener missing bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cielsk committed Apr 9, 2019
1 parent 4d8445d commit dd3241f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ClearableAutoCompleteTextView extends AppCompatAutoCompleteTextView

private boolean mClearIconDrawWhenFocused = false;

private OnTextClearedListener textClearedListener;

public ClearableAutoCompleteTextView(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -102,6 +104,7 @@ public boolean onTouchEvent(MotionEvent event) {
setText(null);
event.setAction(MotionEvent.ACTION_CANCEL);
showClearIcon(false);
if (textClearedListener != null) textClearedListener.onTextCleared();
return false;
}
return super.onTouchEvent(event);
Expand Down Expand Up @@ -135,6 +138,11 @@ private void showClearIcon(boolean show) {
mIsClearIconShown = show;
}

@SuppressWarnings("unused")
public void setOnTextClearedListener(OnTextClearedListener textClearedListener) {
this.textClearedListener = textClearedListener;
}

protected static class ClearIconSavedState extends BaseSavedState {

public static final Creator<ClearIconSavedState> CREATOR =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ClearableTextInputEditText extends TextInputEditText implements Tex

private boolean mClearIconDrawWhenFocused = true;

private OnTextClearedListener textClearedListener;

public ClearableTextInputEditText(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -103,6 +105,7 @@ public boolean onTouchEvent(MotionEvent event) {
setText(null);
event.setAction(MotionEvent.ACTION_CANCEL);
showClearIcon(false);
if (textClearedListener != null) textClearedListener.onTextCleared();
return false;
}
return super.onTouchEvent(event);
Expand Down Expand Up @@ -136,6 +139,11 @@ private void showClearIcon(boolean show) {
mIsClearIconShown = show;
}

@SuppressWarnings("unused")
public void setOnTextClearedListener(OnTextClearedListener textClearedListener) {
this.textClearedListener = textClearedListener;
}

protected static class ClearIconSavedState extends BaseSavedState {

public static final Creator<ClearableTextInputEditText.ClearIconSavedState> CREATOR =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.widget.Toast;
import com.cielyang.android.clearableedittext.ClearableAutoCompleteTextView;
import com.cielyang.android.clearableedittext.ClearableEditText;
import com.cielyang.android.clearableedittext.OnTextClearedListener;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements OnTextClearedListener {

@SuppressLint("SetTextI18n")
@Override protected void onCreate(Bundle savedInstanceState) {
@SuppressLint("SetTextI18n") @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ClearableEditText prepopulatedEditText = findViewById(R.id.prepopulated_edit_text);
prepopulatedEditText.setText("This is pre-populated and has a clear icon");
prepopulatedEditText.setOnTextClearedListener(this);

ClearableEditText emptyEditText = findViewById(R.id.edit_text);
emptyEditText.setOnTextClearedListener(this);

ClearableAutoCompleteTextView autoCompleteTextView =
findViewById(R.id.autocomplete_text_view);
autoCompleteTextView.setOnTextClearedListener(this);
}

@SuppressLint("SetTextI18n") @Override public void onTextCleared() {
Toast.makeText(this, "Cleared text", Toast.LENGTH_LONG).show();
}
}

0 comments on commit dd3241f

Please sign in to comment.