关闭

Android自定义组件EditText,带清空输入内容图标

标签: androidEditText自定义组件
349人阅读 评论(0) 收藏 举报
分类:

自定义EditText组件MyEditText.java

public class MyEditText extends EditText {
    private final String TAG = "MyEditText";
    private Drawable dRight;
    private Rect rBounds;

    public MyEditText(Context paramContext) {
        super(paramContext);
        initEditText();
    }

    public MyEditText(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        initEditText();
    }

    public MyEditText(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        initEditText();
    }

    // 初始化edittext 控件
    private void initEditText() {
        setEditTextDrawable();
        addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
            @Override
            public void afterTextChanged(Editable paramEditable) {
            }

            @Override
            public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
            }

            @Override
            public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
                MyEditText.this.setEditTextDrawable();
            }
        });
    }

    // 控制图片的显示
    public void setEditTextDrawable() {
        if (getText().toString().length() == 0) {
            setCompoundDrawables(null, null, null, null);
        } else {
            setCompoundDrawables(null, null, this.dRight, null);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.dRight = null;
        this.rBounds = null;

    }

    /**
     * 添加触摸事件 点击之后 出现 清空editText的效果
     */
    @Override
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {
            this.rBounds = this.dRight.getBounds();
            int i = (int) paramMotionEvent.getRawX();// 距离屏幕的距离
            // int i = (int) paramMotionEvent.getX();//距离边框的距离
            if (i > getRight() - 3 * this.rBounds.width()) {
                setText("");
                paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.onTouchEvent(paramMotionEvent);
    }

    /**
     * 显示右侧X图片的
     * 
     * 左上右下
     */
    @Override
    public void setCompoundDrawables(Drawable paramDrawable1, Drawable paramDrawable2, Drawable paramDrawable3, Drawable paramDrawable4) {
        if (paramDrawable3 != null)
            this.dRight = paramDrawable3;
        super.setCompoundDrawables(paramDrawable1, paramDrawable2, paramDrawable3, paramDrawable4);
    }
}

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
                android:id="@+id/domainLayout"
                android:layout_width="match_parent"
                android:layout_height="50dip"
                android:orientation="horizontal"
                android:layout_gravity="center"
                >
        <TextView android:layout_width="70dip"
            android:layout_height="match_parent"
            style="@style/text_s28_1c1c1c"
            android:gravity="right|center"
            android:text="用户名:"/>
        <com.ziniu.mobile.module.ui.component.ZiniuEdit
            android:id="@+id/domain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="75dip"
            android:paddingRight="5dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            //文本框为空时提示文字颜色
            android:textColorHint="@color/wechat_text_gray"
            //文本框样式
            style="@style/text_s28_1c1c1c"
            //文本框为空时提示文字
            android:hint="请输入用户名"
            //文本框背景
            android:background="@drawable/edit_text"
            //加载文本框右端删除图标
            android:drawableRight="@drawable/edit_clear_48"
            //设置光标的颜色为@null,表示光标的颜色和输入框的字体颜色相同
            android:textCursorDrawable="@null"/>        

    </RelativeLayout>

</RelativeLayout>

参考:Android自定义EditText输入框带清空按钮

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23954次
    • 积分:732
    • 等级:
    • 排名:千里之外
    • 原创:51篇
    • 转载:6篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论