自定义编辑文本框怎么造呢?

利用Textwatcher观察者来自定义。

首先,我们看下代码:

package com.example.boom.messageproject.ui;import android.content.Context;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;import com.example.boom.messageproject.R;/** * Created by Boom on 2016/8/2. */public class CustomEditText extends EditText  {    Context mcontext;    Drawable img;    public CustomEditText(Context context) {        this(context, null);    }    public CustomEditText(Context context, AttributeSet attrs) {        super(context, attrs);        mcontext = context;        init();    }    private void init() {        this.img = mcontext.getResources().getDrawable(R.mipmap.icon_clear);        this.setSingleLine(true);        this.addTextChangedListener(new CustomTextWatcher());        this.setOnFocusChangeListener(new OnFocusChangeListener() {            @Override            public void onFocusChange(View v, boolean hasFocus) {                setDrawable(hasFocus);            }        });    }    class CustomTextWatcher implements TextWatcher {        @Override        public void beforeTextChanged(CharSequence s, int start, int count, int after) {        }        @Override        public void onTextChanged(CharSequence s, int start, int before, int count) {        }        @Override        public void afterTextChanged(Editable s) {            setDrawable(true);        }    }    private void setDrawable(boolean hasFouce) {        if (length() >= 1 && hasFouce) {            setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);        } else {            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);            ;        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        final int DRAWABLE_RIGHT = 2;        Drawable rightIcon = getCompoundDrawables()[DRAWABLE_RIGHT];        if (rightIcon != null && event.getAction() == MotionEvent.ACTION_UP) {            int leftEdgeOfRightDrawable = getRight() - getPaddingRight()                    - rightIcon.getBounds().width();            if (event.getRawX() >= leftEdgeOfRightDrawable) {                setText("");            }        }        return super.onTouchEvent(event);    }    @Override    protected void finalize() throws Throwable {        img = null;        super.finalize();    }}
功能性代码:

1.设置自定义观察者并添加监察者

 class CustomTextWatcher implements TextWatcher {        @Override        public void beforeTextChanged(CharSequence s, int start, int count, int after) {        }        @Override        public void onTextChanged(CharSequence s, int start, int before, int count) {        }        @Override        public void afterTextChanged(Editable s) {            setDrawable(true);        }    }

 this.addTextChangedListener(new CustomTextWatcher());
2.设置图片位置的显示与不显示,同时包括切换焦点

private void setDrawable(boolean hasFouce) {        if (length() >= 1 && hasFouce) {            setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);        } else {            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);            ;        }    }

        this.setOnFocusChangeListener(new OnFocusChangeListener() {            @Override            public void onFocusChange(View v, boolean hasFocus) {                setDrawable(hasFocus);            }        });


3.处理点击删除图片  删除编辑框的内容

  @Override    public boolean onTouchEvent(MotionEvent event) {        final int DRAWABLE_RIGHT = 2;        Drawable rightIcon = getCompoundDrawables()[DRAWABLE_RIGHT];        if (rightIcon != null && event.getAction() == MotionEvent.ACTION_UP) {            int leftEdgeOfRightDrawable = getRight() - getPaddingRight()                    - rightIcon.getBounds().width();            if (event.getRawX() >= leftEdgeOfRightDrawable) {                setText("");            }        }        return super.onTouchEvent(event);    }
效果图:

Android之利用TextWatcher制作自定义编辑文本框_第1张图片






更多相关文章

  1. Android实现图片的倒影效果分析
  2. android 加载图片轻松避免OOM(out of memory) 支持设置缓存大小,
  3. 下载显示网路图片的例子
  4. android 处理图片之--bitmap处理
  5. android Bimtap 各种图片处理方法、图片特效
  6. Android如何实现图片轮播

随机推荐

  1. Android画画板的制作方法
  2. Android数据存储之SQLite数据库存储
  3. Android系统服务(SystemService)简介
  4. Android(安卓)Drawable Resource学习(六)、
  5. Eclipse和Android(安卓)Studio下使用Goog
  6. Android(安卓)多屏幕适配
  7. [置顶] Google Maps Android(安卓)API V2
  8. Android仿微信加载H5页面进度条
  9. Android(安卓)使用ExoPlayer视频播放 (一)
  10. [置顶] android6.0源码分析之Activity启