带有删除按钮的编辑框

//自定义待删除按钮控件代码

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zk.myappdemo.R;

/**
 * 日期:2017/5/5 13:26
 * 描述:带删除按钮控件
 */

public class DelEditText extends LinearLayout {
    EditText edit;//编辑框
    ImageView image;//右边图片 删除用
    int color;//字体颜色
    float textSize;//字体大小
    String texthint;//提示信息
    int imageId;

    public DelEditText(Context context) {
        super(context, null);
    }

    public DelEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);//初始化 操作
    }

    public DelEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs) {
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.custom_h_line, this, true);
        edit = (EditText) findViewById(R.id.et);
        image = (ImageView) findViewById(R.id.iv_del);
        //获取自定义属性
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.DelEditText);
        color = typeArray.getColor(R.styleable.DelEditText_EditTextColor, Color.BLACK);
        textSize = typeArray.getDimensionPixelSize(R.styleable.DelEditText_EditTextsize1, 15);
        texthint = typeArray.getString(R.styleable.DelEditText_EditHint);
        imageId = typeArray.getResourceId(R.styleable.DelEditText_Image, R.mipmap.del);
        //添加方法
        setEditTextColor(color);//设置颜色
        setEditTextSize(textSize);//设置字体大小
        setEditHint(texthint);//设置提示信息
        setEditImage(imageId);//设置删除图片


        edit.addTextChangedListener(textW);//EditText的输入内容改变监听
        //image view的点击监听
        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edit.setText("");
                image.setVisibility(View.GONE);
            }
        });

        typeArray.recycle();
    }
    /**
     * 日期:2017/5/5 17:16
     * 描述:设置删除按钮图片
     * 作者:hxy
     */
    public void setEditImage(int imageId) {
        image.setImageResource(imageId);
    }
    /**
     * 日期:2017/5/5 17:16
     * 描述:设置输入提示
     * 作者:hxy
     */
    public void setEditHint(String texthint) {
        edit.setHint(texthint);
    }
    /**
     * 日期:2017/5/5 17:16
     * 描述:设置字体大小
     * 作者:hxy
     */
    public void setEditTextSize(float textSize) {
        edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    }
    /**
     * 日期:2017/5/5 17:17
     * 描述:设置字体颜色
     * 作者:hxy
     */
    public void setEditTextColor(int color) {
        edit.setTextColor(color);
    }
    //返回EditText控件
    public EditText getEditText() {
        return edit;
    }
    //返回EditText中输入的内容
    public String getText() {
        return edit.getText().toString();
    }
    //设置EditText中内容
    public void setText(int text) {
        edit.setText(text);
    }
    //设置EditText中内容
    public void setText(String text) {
        edit.setText(text);
    }
    /**
     * 描述:编辑框改变监听
     * 作者:hxy
     */
    TextWatcher textW = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {//没有输入隐藏按钮
                image.setVisibility(View.GONE);
            } else {
                image.setVisibility(View.VISIBLE);
            }
        }
    };
}


布局中使用

<com.zk.myappdemo.view.DelEditText
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/color_control_normal"
            app:EditHint="请输入"
            app:EditTextColor="#0000ff"
            app:EditTextsize1="12sp"/>


自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="DelEditText">
        <attr name="EditTextColor" format="color"/>
        <attr name="EditTextsize1" format="dimension"/>
        <attr name="EditHint" format="string"/>
        <attr name="Image" format="reference"/>
    </declare-styleable>
</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值