Android之利用TextWatcher制作自定义编辑文本框

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

利用Textwatcher观察者来自定义。

首先,我们看下代码:

<span style="font-size:18px;">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();
    }
}
</span>
功能性代码:

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

<span style="font-size:18px;"> 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);
        }
    }
</span>

<span style="font-size:18px;"> this.addTextChangedListener(new CustomTextWatcher());</span>
2.设置图片位置的显示与不显示,同时包括切换焦点

<span style="font-size:18px;">private void setDrawable(boolean hasFouce) {
        if (length() >= 1 && hasFouce) {
            setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            ;
        }
    }</span>

<span style="font-size:18px;">        this.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                setDrawable(hasFocus);
            }
        });</span>


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

<span style="font-size:18px;">  @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);
    }</span>
效果图:







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值