自定义View之可删除内容的EditText

package cn.com.dsyc.canteenappforseller.widget;


import cn.com.dsyc.canteenappforseller.R;
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;


public class ClearEditText extends EditText {

private Drawable mDeleteText;
private OnFocusChangeListener mOnFocusChangeListener;


public ClearEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
if (!isInEditMode())
init();
}


public ClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
if (!isInEditMode())
init();
}


public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
if (!isInEditMode())
init();
}


private void init()
{
mDeleteText = getResources().getDrawable(R.drawable.delete_text);
mDeleteText.setBounds(0, 0, mDeleteText.getIntrinsicWidth(), mDeleteText.getIntrinsicHeight());
setHeight(mDeleteText.getIntrinsicHeight() + getResources().getDimensionPixelSize(R.dimen.one_dp_padding) * 5);
update();

setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (getCompoundDrawables()[2] != null) {
if (event.getAction() == MotionEvent.ACTION_UP && event.getX() > getWidth() - getPaddingRight() - mDeleteText.getIntrinsicWidth()) {
setText("");
update();
}
}
return false;
}
});

addTextChangedListener(new TextWatcher(){


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

}


@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
update();
}


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

}

});

super.setOnFocusChangeListener(new OnFocusChangeListener(){


@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
update();
if (mOnFocusChangeListener != null) 
mOnFocusChangeListener.onFocusChange(v,  hasFocus);
}

});
}


private void update() {
Drawable[] drawables = getCompoundDrawables();
if (getText().length() > 0 && isFocused())
setCompoundDrawables(drawables[0], drawables[1], mDeleteText, drawables[3]);
else
setCompoundDrawables(drawables[0], drawables[1], null, drawables[3]);
}

public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
mOnFocusChangeListener = onFocusChangeListener;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值