一、 attrs.xml 添加新的属性样式
<resources>
<declare-styleable name="EditTextEx">
<attr name="delBk" format="reference|color" />
<attr name="delOffset" format="dimension" />
</declare-styleable>
</resources>
二、java代码,设置新的文本框信息
package com.autonavi.yuexing.view;
import com.autonavi.xmgd.icar2.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class EditTextEx extends EditText {
private Context mContext;
private Drawable mDelBk;
private int mDelOffset;
private ImageView mDelButton;
private boolean mDelButtonFocused;
public EditTextEx(Context context) {
super(context);
mContext = context;
}
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.EditTextEx);
mDelBk = a.getDrawable(R.styleable.EditTextEx_delBk);
mDelOffset = a.getDimensionPixelSize(R.styleable.EditTextEx_delOffset,
8);
a.recycle();
this.addTextChangedListener(mEditWatcher);
creatDelbutton(mContext);
}
private void creatDelbutton(Context context) {
mDelButton = new ImageView(context);
mDelButton.setBackgroundColor(0x00000000);
mDelButton.setImageDrawable(mDelBk);
mDelButton.setOnClickListener(mDelButtonClick);
checkDelButton();
}
public void setDelButtonVisibility(int visibility) {
mDelButton.setVisibility(visibility);
invalidate();
}
OnClickListener mDelButtonClick = new OnClickListener() {
@Override
public void onClick(View v) {
setText("");
invalidate();
}
};
TextWatcher mEditWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
checkDelButton();
}
};
private void checkDelButton() {
String textEdit = getText().toString();
mDelButton.setVisibility(TextUtils.isEmpty(textEdit) ? View.GONE
: View.VISIBLE);
}
public void setDelDrawable(Drawable drawable) {
mDelBk = drawable;
mDelButton.setBackgroundColor(0x00000000);
mDelButton.setImageDrawable(mDelBk);
invalidate();
}
public Drawable getDelDrawable() {
return mDelBk;
}
public void setDelOff(int offset) {
mDelOffset = offset;
}
public int getDelOff() {
return mDelOffset;
}
@Override
public int getCompoundPaddingRight() {
Rect paddingBk = new Rect();
getBackground().getPadding(paddingBk);
return super.getCompoundPaddingRight() + mDelButton.getWidth()
+ mDelOffset - paddingBk.right;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mDelButton.measure(0, 0);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
int edit_w = getWidth();
int edit_h = getHeight();
int delButton_w = mDelButton.getMeasuredWidth();
int delButton_h = mDelButton.getMeasuredHeight();
mDelButton.layout(edit_w - delButton_w - mDelOffset,
(edit_h - delButton_h) / 2, edit_w - mDelOffset, edit_h
- (edit_h - delButton_h) / 2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDelButton.getVisibility() != View.GONE
&& mDelButton.getVisibility() != View.INVISIBLE) {
canvas.save();
canvas.translate(getScrollX() + mDelButton.getLeft(), getScrollY()
+ mDelButton.getTop());
mDelButton.draw(canvas);
canvas.restore();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (isDelButtonHit(event)) {
mDelButtonFocused = true;
if (!mDelButton.isPressed()) {
mDelButton.setPressed(true);
invalidate();
}
return true;
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (mDelButtonFocused) {
boolean hit = isDelButtonHit(event);
if (mDelButton.isPressed() != hit) {
mDelButton.setPressed(hit);
invalidate();
}
return true;
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
if (mDelButtonFocused) {
boolean hit = isDelButtonHit(event);
if (mDelButton.isPressed() != hit) {
mDelButton.setPressed(hit);
invalidate();
}
mDelButtonFocused = false;
return true;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (mDelButtonFocused) {
if (mDelButton.isPressed() && mDelButtonClick != null)
mDelButtonClick.onClick(mDelButton);
if (mDelButton.isPressed()) {
mDelButton.setPressed(false);
invalidate();
}
mDelButtonFocused = false;
return mDelButton.isPressed();
}
}
return super.dispatchTouchEvent(event);
}
private boolean isDelButtonHit(MotionEvent event) {
final Rect frame = new Rect();
mDelButton.getHitRect(frame);
return frame.contains((int) event.getX(), (int) event.getY());
}
}
三、布局文件调用自定义文本框
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:self="http://schemas.android.com/apk/res/com.autonavi.xmgd.icar2" // 自己的包名
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#EBEBEB">
<com.autonavi.yuexing.view.EditTextEx
self:delBk="@drawable/del" //自定义属性
android:id="@+id/navi_res_start"
android:hint="请输入起点 "
android:singleLine="true"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:minWidth="200dip"
android:textSize="20dip"
android:textColor="#151515" />
<com.autonavi.yuexing.view.EditTextEx
self:delBk="@drawable/del"
android:id="@+id/navi_res_end"
android:hint="请输入终点 "
android:singleLine="true"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20dip"
android:textColor="#151515" />
</LinearLayout>