android自定義文字框,後面帶清空按鈕
阿新 • • 發佈:2019-01-01
class MyEditText extends EditText {
private final String TAG = "MyEditText";
private Drawable dRight;
private Rect rBounds;
// 構造器
public MyEditText(Context paramContext) {
super(paramContext);
initEditText();
}
public MyEditText(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet) ;
initEditText();
}
public MyEditText(Context paramContext, AttributeSet paramAttributeSet,
int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
initEditText();
}
// 初始化edittext 控制元件
@SuppressLint("ResourceAsColor")
private void initEditText() {
setEditTextDrawable();
this.setBackgroundResource (R.drawable.edittext_bg);
this.setTextColor(R.color.login_txt);
this.setHintTextColor(R.color.login_remind_txt);
this.setPadding(20, 3, 6, 3);
this.setTextSize(14);
this.setSingleLine(true);
addTextChangedListener(new TextWatcher() { // 對文字內容改變進行監聽
public void afterTextChanged(Editable paramEditable) {
}
public void beforeTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
}
public void onTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
MyEditText.this.setEditTextDrawable();
}
});
}
// 控制圖片的顯示
private void setEditTextDrawable() {
if (getText().toString().length() == 0){
setCompoundDrawables(null, null, null, null);
} else {
setCompoundDrawables(null, null, this.dRight, null);
}
}
//失去焦點事件
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
//獲得焦點,判斷是否有內容
if (this.hasFocus() == true) {
//如果沒有內容,則保護顯示清空按鈕,否則就顯示
if (getText().toString().length() == 0){
setCompoundDrawables(null, null, null, null);
} else {
setCompoundDrawables(null, null, this.dRight, null);
}
}else{
//失去焦點,隱藏按鈕
setCompoundDrawables(null, null, null, null);
}
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
protected void finalize() throws Throwable {
super.finalize();
this.dRight = null;
this.rBounds = null;
}
// 新增觸控事件
@SuppressWarnings("unused")
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
if ((this.dRight != null) && (paramMotionEvent.getAction() == 1) && getText().toString().length() > 0) {
this.rBounds = this.dRight.getBounds();
int i = (int) paramMotionEvent.getX();
int ix = (int) paramMotionEvent.getRawX();
// int e = (int)this.getX();
// int ex = (int)this.getRotationX();
int width = this.getWidth();
int left = (int)this.getLeft();
int right = (int)this.getRight();
// int marginWidth = right - this.rBounds.width()-10;
int marginWidth = width - this.rBounds.width()-5;
// int marginWidth = i - (getRight() - 3 * (this.rBounds.width()+10));
// if (marginWidth > -40 && marginWidth < 40) {
if (marginWidth < i && i <= width) {
setText("");
paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
}
}
return super.onTouchEvent(paramMotionEvent);
}
// 設定顯示的圖片資源
public void setCompoundDrawables(Drawable paramDrawable1,
Drawable paramDrawable2, Drawable paramDrawable3,
Drawable paramDrawable4) {
if (paramDrawable3 != null)
this.dRight = paramDrawable3;
super.setCompoundDrawables(paramDrawable1, paramDrawable2,
paramDrawable3, paramDrawable4);
}
}