带清除按钮的EditText~

上一篇写了个键盘,还差个清除按钮的输入框~发现一个拿来能用的就补上了~

先上图~

请无视这个渣渣删除按钮,临时做的个~~~

 

上代码~     MyCustomEditText.JAVA   

package com.ob.main;

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.view.MotionEvent;
import android.widget.EditText;

public class MyCustomEditText extends EditText {

	private Drawable dRight;
	private Rect rBounds;//用来存储成对出现的对象,比如矩形的左上角坐标、宽度、高度~

	// 构造器
	public MyCustomEditText(Context context) {
		super(context);
		init();
	}

	public MyCustomEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyCustomEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		setEditTextDrawable();
		// 对文字的改变进行监听
		addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				MyCustomEditText.this.setEditTextDrawable();
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	// 控制图片的显示
	private void setEditTextDrawable() {
		if (getText().toString().length() == 0) {
			setCompoundDrawables(null, null, null, null);
		} else {
			setCompoundDrawables(null, null, this.dRight, null);
		}
	}
	// 添加触摸事件
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
            if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {
                    this.rBounds = this.dRight.getBounds();
                    int i = (int) paramMotionEvent.getX();
                    if (i > getRight() - 3 * this.rBounds.width()) {
                            setText("");
                            paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
                    }
            }
            return super.onTouchEvent(paramMotionEvent);
    }

   // 设置显示的图片资源
    public void setCompoundDrawables(Drawable paramDrawable1,
                    Drawable paramDrawable2, Drawable paramDrawable3,
                    Drawable paramDrawable4) {
            if (paramDrawable3 != null)
                    this.dRight = paramDrawable3;
            super.setCompoundDrawables(paramDrawable1, paramDrawable2,
                            paramDrawable3, paramDrawable4);
    }

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


}

触摸事件中Rect对象起了很大的作用,节省了很多代码量...学习之~

 

main.xml直接加上~

 

<com.ob.main.MyCustomEditText
        android:id="@+id/et_money_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/edittext_delete"
        android:layout_above="@id/kerboard_num"
        android:singleLine="true" />


原文:http://www.eoeandroid.com/thread-210369-1-1.html

 

详细参考:http://www.it165.net/pro/html/201405/13326.html

 

共勉~

键盘和输入框的下载地址~http://download.csdn.net/detail/pds574834424/8141937

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值