直接上代码,呵呵,以下的
setCompoundDrawables是最关键的方法。这里要先获取布局文件里面的drawableRight的图片传给dRight。
package com.example.android_widget_byself_widget; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; public class EdittextCanClear extends EditText { private Drawable dRight; private Drawable dLeft; private Rect rect; public EdittextCanClear(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } public EdittextCanClear(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(); } public EdittextCanClear(Context context) { super(context); // TODO Auto-generated constructor stub init(); } // 初始化edittext 控件 private void init() { // TODO Auto-generated method stub setEditTextImage(); this.setOnFocusChangeListener(new Changlistener()); // 内容监听 addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub EdittextCanClear.this.setEditTextImage(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (this.dRight != null && event.getAction() == 1 && this.isFocused()) { rect = this.dRight.getBounds(); int i = (int) event.getX(); if (i > this.getRight() - 2 * rect.width() - getPaddingRight() && i < getRight() - getPaddingRight()) { setText(""); event.setAction(MotionEvent.ACTION_CANCEL); } } return super.onTouchEvent(event); } // 控制图片的显示 private void setEditTextImage() { if (getText().toString().length() != 0 && this.isFocused()) { setCompoundDrawables(dLeft, null, this.dRight, null); } else { setCompoundDrawables(dLeft, null, null, null); } } // /** // * 设置显示的图片资源 // */ // private void setCompoundDrawable(Drawable left, Drawable top, // Drawable right, Drawable bottom) { // if (right != null && this.dRight == null) { // this.dRight = right; // } // this.dLeft = left; // super.setCompoundDrawables(left, top, right, bottom); // } @Override public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { // TODO Auto-generated method stub if (right != null && this.dRight == null) { this.dRight = right; } this.dLeft = left; super.setCompoundDrawables(left, top, right, bottom); } // 清楚缓存 @Override protected void finalize() throws Throwable { super.finalize(); this.dRight = null; this.rect = null; this.dLeft = null; } class Changlistener implements OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub EdittextCanClear.this.setEditTextImage(); } } }
自定义edittext,用于带删除的输入框和搜索框
最新推荐文章于 2024-03-23 03:52:11 发布