带删除按钮的EditText 带密码显示隐藏

本文介绍了一种自定义EditText组件的方法,该组件在输入框内集成了一个删除按钮,当输入框获得焦点且有输入内容时显示。此外,还支持切换密码可见性的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.iim.ego.widget;


import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.iim.ego.R;

import java.lang.reflect.Field;

/**
 * Created by cjf on 2018-04-10 11:36
 */
public class EditTextWithDel extends EditText implements View.OnFocusChangeListener {

    private Drawable imgAble;
    private Context mContext;
    //点击区域大小
    private int clickArea = 100;
    //是否为密码类型
    private boolean isPassword = false;
    //当前是否可查看密码
    private boolean isEyeOne = false;


    public EditTextWithDel(Context context) {
        super(context);
        mContext = context;
        init();
//      this(context,null);
    }

    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
//      this(context,attrs,0);
    }

    public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }


    private void init() {
        setHintTextColor(getResources().getColor(R.color.color_bg_cccccc));
        setTextColor(getResources().getColor(R.color.color_bg_333333));
        setGravity(Gravity.CENTER_VERTICAL);
        setCursorColor();
        imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_clear);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setDrawable();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        setOnFocusChangeListener(this);
    }

    //设置删除图片
    public void setDrawable() {
        if (hasFocus()) {
            if (length() < 1) {
                setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

            } else {
                setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
            }
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }

    }

    // 处理右边图标点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgAble != null && event.getAction() == MotionEvent.ACTION_DOWN && hasFocus()) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Rect rect = new Rect();
            int[] location = new int[2];
            getLocationOnScreen(location);
            rect.right = location[0] + getWidth();
            rect.left = rect.right - clickArea;
            rect.top = location[1];
            rect.bottom = rect.top + getHeight();
            if (rect.contains(eventX, eventY)) {//点击图标
                if (isPassword) {//密码类型
                    if (isEyeOne) {
                        setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        isEyeOne = false;
                    } else {
                        setInputType(InputType.TYPE_CLASS_TEXT);
                        isEyeOne = true;
                    }
                } else {//普通类型 删除文字
                    setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {//有焦点
            setDrawable();
            ((EditTextWithDel) view).setSelection(((EditTextWithDel) view).getText().length());
        } else {//失去焦点
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    public int getClickArea() {
        return clickArea;
    }

    /**
     * 设置点击区域大小
     *
     * @param clickArea
     */
    public void setClickArea(int clickArea) {
        this.clickArea = clickArea;
    }


    /**
     * 设置光标颜色值
     */
    public void setCursorColor() {
        try {
            Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
            f.setAccessible(true);
            f.set(this, R.drawable.shape_cursor_color);
        } catch (Exception ignored) {
        }
    }


    @Override
    public void setInputType(int type) {
        super.setInputType(type);
        if (type == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
            isPassword = true;
            imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_setting_eye_off);
        } else {
            if (isPassword) {
                imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_setting_eye_on);
            }
        }
        setDrawable();
    }
}

光标样式shape_cursor_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/color_text_3582da"/>
    <size android:width="1dp"/>
</shape>

btn_equip_clear
这里写图片描述

btn_equip_setting_eye_off
这里写图片描述

btn_equip_setting_eye_on
这里写图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值