Android 带删除按钮的EdiText

自定义EdiText带删除效果
效果图:
这里写图片描述
自定义EdtiText

package com.example.liushuaitao.a01edtitextwithdel.MyEditTextWithDel;

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;

import com.example.liushuaitao.a01edtitextwithdel.R;

/**
 * Created by liushuaitao on 2016/7/13.
 */

public class EditTextWithDel extends EditText {

    private String TAG = "Tao";
    private Context context;
    private Drawable imgInable;
    private Drawable imgAble;
    /**
     * @param context
     */
    public EditTextWithDel(Context context) {
        super(context);
        this.context = context;
        init();
    }


    /**
     * @param context
     * @param attrs
     */
    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    /**
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public EditTextWithDel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }


    /**
     *
     */
    public void init() {
        imgInable = context.getResources().getDrawable(R.drawable.delete_gray);
        imgAble = context.getResources().getDrawable(R.drawable.delete);
        this.addTextChangedListener(new TextWatcher() {
            /**
             *
             * @param s
             * @param start
             * @param count
             * @param after
             */
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            /**
             *
             * @param s
             * @param start
             * @param before
             * @param count
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            /**
             *
             * @param s
             */
            @Override
            public void afterTextChanged(Editable s) {
                SetDeawable();

            }
        });

        SetDeawable();

    }

    /**
     *
     */
    public void SetDeawable() {
        if (this.length() <=0) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
        } else {
            this.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();

            this.getGlobalVisibleRect(rect);
            rect.left = rect.right - imgAble.getMinimumWidth();
            if (rect.contains(eventX, eventY)) {

                Log.d(TAG, "删除效果");
                this.setText("");

            }
        }

        return super.onTouchEvent(event);


    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();

    }
}

Layout布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.liushuaitao.a01edtitextwithdel.MainActivity">

    <com.example.liushuaitao.a01edtitextwithdel.MyEditTextWithDel.EditTextWithDel
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:background="@drawable/ed_bg"
        android:padding="10dp"
        android:hint="请输入密码"/>
</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值