Skip to content

Instantly share code, notes, and snippets.

@SyllaJay
Created July 14, 2015 02:22
Show Gist options
  • Save SyllaJay/63bdb37fa0b7617975db to your computer and use it in GitHub Desktop.
Save SyllaJay/63bdb37fa0b7617975db to your computer and use it in GitHub Desktop.
Android Clearable EditText
package com.tryear.jandroidlib.widget;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
/**
* <p/>
* Created by JAY on 2015/4/18.
*/
public class ClearableEditText extends EditText implements View.OnTouchListener,
View.OnFocusChangeListener, TextWatcher {
public interface ClearListener {
void didClearText();
}
public void setClearListener(ClearListener clearListener) {
this.clearListener = clearListener;
}
private Drawable clearDrawable;
private ClearListener clearListener;
public ClearableEditText(Context context) {
super(context);
init();
}
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void setOnTouchListener(OnTouchListener l) {
this.l = l;
}
@Override
public void setOnFocusChangeListener(OnFocusChangeListener f) {
this.f = f;
}
private OnTouchListener l;
private OnFocusChangeListener f;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getCompoundDrawables()[2] != null) {
boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - clearDrawable
.getIntrinsicWidth());
if (tappedX) {
if (event.getAction() == MotionEvent.ACTION_UP) {
setText("");
if (clearListener != null) {
clearListener.didClearText();
}
}
return true;
}
}
if (l != null) {
return l.onTouch(v, event);
}
return false;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(isNotEmpty(getText()));
} else {
setClearIconVisible(false);
}
if (f != null) {
f.onFocusChange(v, hasFocus);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (isFocused()) {
setClearIconVisible(isNotEmpty(s));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
private void init() {
clearDrawable = getCompoundDrawables()[2];
if (clearDrawable == null) {
clearDrawable = getResources()
.getDrawable(android.R.drawable.presence_offline);
}
clearDrawable.setBounds(0, 0, clearDrawable.getIntrinsicWidth(), clearDrawable.getIntrinsicHeight());
setClearIconVisible(false);
super.setOnTouchListener(this);
super.setOnFocusChangeListener(this);
addTextChangedListener(this);
}
protected void setClearIconVisible(boolean visible) {
boolean wasVisible = (getCompoundDrawables()[2] != null);
if (visible != wasVisible) {
Drawable x = visible ? clearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], x, getCompoundDrawables()[3]);
}
}
private boolean isNotEmpty(CharSequence str) {
return !TextUtils.isEmpty(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment