自定义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>