android edittext 叉,带叉号的EditText

package com.ds.uilib.edittext;

import android.content.Context;

import android.graphics.Rect;

import android.graphics.drawable.Drawable;

import android.text.Editable;

import android.text.TextWatcher;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;

import com.ds.uilib.R;

/**

Created by Sai on 2018/3/27.

带删除的EditTextView

*/

public class EditTextWithDelView extends AppCompatEditText implements View.OnFocusChangeListener,TextWatcher {

/**

* 删除按钮的引用

*/

private Drawable mClearDrawable;

private Context context;

/**

* 控件是否有焦点

*/

private boolean hasFocus;

public EditTextWithDelView(Context context) {

this(context,null);

// super(context);

// this.context = context;

// init();

}

public EditTextWithDelView(Context context, AttributeSet attrs){

//这里构造方法也很重要,不加这个很多属性不能再XML里面定义

this(context, attrs, android.R.attr.editTextStyle);

}

public EditTextWithDelView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

private void init() {

mClearDrawable = getCompoundDrawables()[2];

if (mClearDrawable == null) {

//自定义的叉号图标

mClearDrawable = getResources().getDrawable(R.drawable.ic_fork);

}

mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

//默认设置隐藏图标

setClearIconVisible(false);

setOnFocusChangeListener(this);

addTextChangedListener(this);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (mClearDrawable != null && event.getAction() == MotionEvent.ACTION_UP) {

int x = (int) event.getX();

boolean isInnerWidth = (x > (getWidth() - getTotalPaddingRight())) &&

(x < (getWidth() - getPaddingRight()));

Rect rect = mClearDrawable.getBounds();

int height = rect.height();

int y = (int) event.getY();

int distance = (getHeight() - height) / 2;

boolean isInnerHeight = (y > distance) && (y < (distance + height));

if (isInnerHeight && isInnerWidth) {

this.setText("");

}

}

return super.onTouchEvent(event);

}

/**

* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去

*

* @param visible

*/

public void setClearIconVisible(boolean visible) {

Drawable right = visible ? mClearDrawable : null;

setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],

right, getCompoundDrawables()[3]);

}

/**

* 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏

*/

@Override

public void onFocusChange(View v, boolean hasFocus) {

this.hasFocus = hasFocus;

if (hasFocus) {

setClearIconVisible(getText().length() > 0);

} else {

setClearIconVisible(false);

}

}

/**

* 当输入框里面内容发生变化的时候回调的方法

*/

@Override

public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

if (hasFocus) {

setClearIconVisible(text.length() > 0);

}

}

@Override

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

}

@Override

public void afterTextChanged(Editable s) {

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值