EditTextWithDel组件的功能如下:
1、在没用内容的时候显示不可用的图片状态,在有内容的时候显示可用的图片状态;
2、在有内容的时候点击删除按钮可以删除EditText中的内容;
组件的代码如下:
[java]view plaincopyprint?
- package com.sunday.customs;
- import com.example.customs.R;
- 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.util.Log;
- import android.view.MotionEvent;
- import android.widget.EditText;
- /**
- * @author sunday
- * 2013-12-04
- */
- publicclass EditTextWithDel extends EditText {
- privatefinalstatic String TAG = "EditTextWithDel";
- private Drawable imgInable;
- private Drawable imgAble;
- private Context mContext;
- public EditTextWithDel(Context context) {
- super(context);
- mContext = context;
- init();
- }
- public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- mContext = context;
- init();
- }
- public EditTextWithDel(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext = context;
- init();
- }
- privatevoid init() {
- imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray);
- imgAble = mContext.getResources().getDrawable(R.drawable.delete);
- addTextChangedListener(new TextWatcher() {
- @Override
- publicvoid onTextChanged(CharSequence s, int start, int before, int count) {}
- @Override
- publicvoid beforeTextChanged(CharSequence s, int start, int count, int after) {}
- @Override
- publicvoid afterTextChanged(Editable s) {
- setDrawable();
- }
- });
- setDrawable();
- }
- //设置删除图片
- privatevoid setDrawable() {
- if(length() < 1)
- setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
- else
- setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
- }
- // 处理删除事件
- @Override
- publicboolean onTouchEvent(MotionEvent event) {
- if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
- int eventX = (int) event.getRawX();
- int eventY = (int) event.getRawY();
- Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
- Rect rect = new Rect();
- getGlobalVisibleRect(rect);
- rect.left = rect.right - 50;
- if(rect.contains(eventX, eventY))
- setText("");
- }
- returnsuper.onTouchEvent(event);
- }
- @Override
- protectedvoid finalize() throws Throwable {
- super.finalize();
- }
- }
package com.sunday.customs; import com.example.customs.R; 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.util.Log; import android.view.MotionEvent; import android.widget.EditText; /** * @author sunday * 2013-12-04 */ public class EditTextWithDel extends EditText { private final static String TAG = "EditTextWithDel"; private Drawable imgInable; private Drawable imgAble; private Context mContext; public EditTextWithDel(Context context) { super(context); mContext = context; init(); } public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } public EditTextWithDel(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } private void init() { imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray); imgAble = mContext.getResources().getDrawable(R.drawable.delete); addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) { setDrawable(); } }); setDrawable(); } //设置删除图片 private void setDrawable() { if(length() < 1) setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null); else setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null); } // 处理删除事件 @Override public boolean onTouchEvent(MotionEvent event) { if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) { int eventX = (int) event.getRawX(); int eventY = (int) event.getRawY(); Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY); Rect rect = new Rect(); getGlobalVisibleRect(rect); rect.left = rect.right - 50; if(rect.contains(eventX, eventY)) setText(""); } return super.onTouchEvent(event); } @Override protected void finalize() throws Throwable { super.finalize(); } }
效果图如下:
添加删除功能的EditText
时间: 2024-10-12 22:30:47