自定义View之自定义EditText带删除内容按钮控件

  • Post author:
  • Post category:其他


自定义View之自定义EditText带删除内容按钮控件

简单的自定义控件,效果如图(文件压缩后变得不清晰了)


1、新增一个设置删除图片的添加属性,在attrs文件中设置如下

    <!--edittext-->
    <declare-styleable name="DeleteContent_Edit">
        <attr name="deleteImg" format="reference" />
    </declare-styleable>

2、编写继承EditText的自定义控件,代码如下,

package com.cheng.cc.customcontrols.views;

import android.content.Context;
import android.content.res.TypedArray;
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;

import com.cheng.cc.customcontrols.R;

/**
 * @author Created by cc on 17/6/4.
 * @fileName DeleteContent_Edit
 * @githublink https://github.com/cc0819
 * @csdnlink http://blog.csdn.net/qq_25404567
 */

public class DeleteContent_Edit extends EditText {

    private Context mContext;
    private Drawable deleteImg;



    public DeleteContent_Edit(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    public DeleteContent_Edit(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs);
    }


    private void init(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.DeleteContent_Edit);
        deleteImg = typedArray.getDrawable(R.styleable.DeleteContent_Edit_deleteImg);
        typedArray.recycle();
        if (deleteImg != null){//判断是否添加了删除按钮
            addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    setDeleteImg();
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });
        }

    }

    private void setDeleteImg() {//设置删除按钮,输入字符串大于1时显示
        if (length()< 1){
            setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
        }else {
            setCompoundDrawablesWithIntrinsicBounds(null,null,deleteImg,null);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (deleteImg != null && event.getAction() == MotionEvent.ACTION_UP){//对内容清空
            getText().clear();
        }
        return super.onTouchEvent(event);
    }
}

3、在布局中引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.cheng.cc.customcontrols.activity.EditDeleteContent_Activity">

    <com.cheng.cc.customcontrols.views.DeleteContent_Edit
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:deleteImg="@mipmap/delete"//需要清除按钮时候才添加
        />


</LinearLayout>


github下载地址(更新学习中):

https://github.com/cc0819/CustomControls




版权声明:本文为qq_25404567原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。