帶刪除功能的EditText

文本輸入框如果需要后邊帶個(gè)刪除按鈕的話(huà),可以用下邊的自定義控件。
只要設(shè)置如下即可。

QQ截圖20171025142511.png

原理很簡(jiǎn)單,就是取drawableRight的圖片,監(jiān)聽(tīng)觸摸事件,看點(diǎn)擊的位置是不是drawableRight圖片的位置,是的話(huà)就執(zhí)行刪除操作,也就是把文本置空,setText=""而已.

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 *
 * @author Sage
 *  2017/4/19
 */

public class EditTextRightClear extends AppCompatEditText {
    public EditTextRightClear(Context context) {
        super(context);
        initEditText();
    }

    public EditTextRightClear(Context context, AttributeSet attrs) {
        super(context, attrs);
        initEditText();
    }

    public EditTextRightClear(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initEditText();
    }
    private Drawable dBottom;
    private Drawable dRight;
    private Rect rBounds;

    private void initEditText() {
        setEditTextDrawable();
        setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                setEditTextDrawable();
            }
        });
        addTextChangedListener( new 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) {
                setEditTextDrawable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    /**控制圖片的顯示*/
    private void setEditTextDrawable() {
        if (isFocused()) {
            if (getText().toString().length() == 0) {
                setCompoundDrawables(null, null, null, this.dBottom);
            } else {
                setCompoundDrawables(null, null, this.dRight, this.dBottom);
            }
        } else{
            setCompoundDrawables(null, null, null, this.dBottom);
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        this.dRight = null;
        this.dBottom = null;
        this.rBounds = null;
    }

    // 添加觸摸事件
    @Override
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (paramMotionEvent.getAction() == MotionEvent.ACTION_UP && dRight != null) {
            rBounds = dRight.getBounds();
            float x = paramMotionEvent.getRawX();
            int width = this.getRight();

            if (x >= (width - rBounds.width() - getPaddingRight())) {
                this.setText("");
                paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.onTouchEvent(paramMotionEvent);
    }

    // 設(shè)置顯示的圖片資源
    @Override
    public void setCompoundDrawables(Drawable paramDrawable1, Drawable paramDrawable2,
                                     Drawable paramDrawable3, Drawable paramDrawable4) {
        if (paramDrawable3 != null) {

            this.dRight = paramDrawable3;
        }
        if (paramDrawable4 != null) {
            this.dBottom = paramDrawable4;
        }
        super.setCompoundDrawables(paramDrawable1, paramDrawable2, paramDrawable3, paramDrawable4);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.說(shuō)明 自定義帶刪除功能的EditText有兩種方法,第一種是用組合視圖的方法,即在一個(gè)view視圖里面左側(cè)放置...
    AiPuff閱讀 1,175評(píng)論 0 2
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線(xiàn)程,因...
    小菜c閱讀 7,362評(píng)論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,432評(píng)論 4 61
  • 《裕語(yǔ)言》速成開(kāi)發(fā)手冊(cè)3.0 官方用戶(hù)交流:iApp開(kāi)發(fā)交流(1) 239547050iApp開(kāi)發(fā)交流(2) 10...
    葉染柒丶閱讀 28,775評(píng)論 5 20
  • 2016,上路者與探路者 文/鄒航 2016,在沒(méi)有太多告別與釋?xiě)阎芯瓦@樣過(guò)去了。說(shuō)實(shí)話(huà),這一年,自己是在極為平淡...
    鄒航閱讀 368評(píng)論 0 3

友情鏈接更多精彩內(nèi)容