128 lines
4.0 KiB
Java
128 lines
4.0 KiB
Java
package com.ubt.jimu.widgets;
|
|
|
|
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;
|
|
import com.ubt.jimu.R;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
|
|
private Drawable a;
|
|
private boolean b;
|
|
private Callback c;
|
|
private onFocuschangeListener d;
|
|
|
|
public interface Callback {
|
|
void a();
|
|
}
|
|
|
|
public interface onFocuschangeListener {
|
|
void a(boolean z);
|
|
}
|
|
|
|
public ClearEditText(Context context) {
|
|
this(context, null);
|
|
}
|
|
|
|
private void a() {
|
|
this.a = getCompoundDrawables()[2];
|
|
if (this.a == null) {
|
|
this.a = getResources().getDrawable(R.drawable.ic_edit_delete);
|
|
}
|
|
Drawable drawable = this.a;
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), this.a.getIntrinsicHeight());
|
|
setClearIconVisible(false);
|
|
setOnFocusChangeListener(this);
|
|
addTextChangedListener(this);
|
|
setImeOptions(33554438);
|
|
}
|
|
|
|
@Override // android.text.TextWatcher
|
|
public void afterTextChanged(Editable editable) {
|
|
Callback callback;
|
|
if (!TextUtils.isEmpty(editable.toString().trim()) || (callback = this.c) == null) {
|
|
return;
|
|
}
|
|
callback.a();
|
|
}
|
|
|
|
@Override // android.text.TextWatcher
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
|
}
|
|
|
|
@Override // android.view.View.OnFocusChangeListener
|
|
public void onFocusChange(View view, boolean z) {
|
|
this.b = z;
|
|
if (z) {
|
|
setClearIconVisible(getText().length() > 0);
|
|
} else {
|
|
setClearIconVisible(false);
|
|
}
|
|
onFocuschangeListener onfocuschangelistener = this.d;
|
|
if (onfocuschangelistener != null) {
|
|
onfocuschangelistener.a(z);
|
|
}
|
|
}
|
|
|
|
@Override // android.widget.TextView, android.view.View
|
|
protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
|
|
super.onLayout(z, i, i2, i3, i4);
|
|
}
|
|
|
|
@Override // android.widget.TextView, android.view.View
|
|
protected void onMeasure(int i, int i2) {
|
|
super.onMeasure(i, i2);
|
|
int height = getHeight();
|
|
int intrinsicHeight = this.a.getIntrinsicHeight();
|
|
int i3 = height / 2;
|
|
if (intrinsicHeight <= i3) {
|
|
i3 = intrinsicHeight;
|
|
}
|
|
this.a.setBounds(0, 0, i3, i3);
|
|
}
|
|
|
|
@Override // android.widget.TextView, android.text.TextWatcher
|
|
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
|
if (this.b) {
|
|
setClearIconVisible(charSequence.length() > 0);
|
|
}
|
|
}
|
|
|
|
@Override // android.widget.TextView, android.view.View
|
|
public boolean onTouchEvent(MotionEvent motionEvent) {
|
|
if (motionEvent.getAction() == 1 && getCompoundDrawables()[2] != null) {
|
|
if (motionEvent.getX() > ((float) (getWidth() - getTotalPaddingRight())) && motionEvent.getX() < ((float) (getWidth() - getPaddingRight()))) {
|
|
setText("");
|
|
}
|
|
}
|
|
return super.onTouchEvent(motionEvent);
|
|
}
|
|
|
|
public void setCallback(Callback callback) {
|
|
this.c = callback;
|
|
}
|
|
|
|
public void setClearIconVisible(boolean z) {
|
|
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], z ? this.a : null, getCompoundDrawables()[3]);
|
|
}
|
|
|
|
public void setOnFocuschangeListener(onFocuschangeListener onfocuschangelistener) {
|
|
this.d = onfocuschangelistener;
|
|
}
|
|
|
|
public ClearEditText(Context context, AttributeSet attributeSet) {
|
|
this(context, attributeSet, android.R.attr.editTextStyle);
|
|
}
|
|
|
|
public ClearEditText(Context context, AttributeSet attributeSet, int i) {
|
|
super(context, attributeSet, i);
|
|
a();
|
|
}
|
|
}
|