DeleteEditTextView.java 2.8 KB
Newer Older
1
package com.ydl.ydlcommon.view;
konghaorui committed
2 3 4 5 6 7 8 9 10 11 12

import android.annotation.SuppressLint;
import android.content.Context;
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.yidianling.common.tools.RxImageTool;
13
import com.ydl.ydlcommon.R;
konghaorui committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


/**
 * 右边有删除按钮的edit text
 * Created by Dog on 2015/6/29.
 */
@SuppressLint("AppCompatCustomView")
public class DeleteEditTextView extends EditText {

    int mCloseAreaSize;

    public DeleteEditTextView(Context context) {
        super(context);
        initRootView();
    }

    public DeleteEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRootView();
    }

    public DeleteEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initRootView();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        setSelection(text.length());
    }

    /**
     * 初始化视图
     */
    void initRootView() {
        mCloseAreaSize = RxImageTool.dp2px(30);

konghaorui committed
52
        int padding = getResources().getDimensionPixelSize(R.dimen.platform_default_dis_size_small);
konghaorui committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        setCompoundDrawablePadding(padding);
        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) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                setEditTextDrawable();
            }
        });

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

    void setEditTextDrawable() {
        if (getText().length() == 0 || !isFocused()) {
            setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
        } else {
konghaorui committed
83
            setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, getContext().getResources().getDrawable(R.drawable.platform_delete), null);
konghaorui committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if ((event.getAction() == 1) && isFocused()) {
            int i = (int) event.getRawX();
            if (i > getRight() - mCloseAreaSize) {
                setText("");
                event.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.onTouchEvent(event);
    }


}