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

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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值