简单实现EditText右侧删除按钮

和上一篇文章大致一样,只是做了简单的修改,监听文字输入,当有文字输入时,显示右侧删除按钮,同时EditText#setTouchListener,当右侧删除 Drawable 被点击时,清空 EditText。这里还是使用的 AutoCompleteTextView 来实现。

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnTouchListener, TextWatcher {

    private List<String> mData = new ArrayList<>();

    {
        for (int i = 0; i < 15; i++) {
            mData.add("item: " + i);
        }
        for (int i = 0; i < 15; i++) {
            mData.add("tttt: " + i);
        }
    }

    private AutoCompleteTextView mAutoCompleteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view);
        mAutoCompleteTextView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, mData));

        mAutoCompleteTextView.setOnTouchListener(this);
        mAutoCompleteTextView.addTextChangedListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        // getCompoundDrawablesRelative() 可以获取一个长度为4的数组,
        // 存放drawableStart,Top,End, Bottom四个图片资源对象
        // index=2 表示的是 drawableEnd 图片资源对象
        Drawable drawable = mAutoCompleteTextView.getCompoundDrawablesRelative()[2];
        if (drawable == null) {
            return false;
        }

        if (event.getAction() != MotionEvent.ACTION_UP) {
            return false;
        }

        // drawable.getIntrinsicWidth() 获取drawable资源图片呈现的宽度
        if (event.getX() > mAutoCompleteTextView.getWidth() - mAutoCompleteTextView.getPaddingRight()
                - drawable.getIntrinsicWidth()) {
            // 进入这表示图片被选中,可以处理相应的逻辑了
            mAutoCompleteTextView.setText("");
        }

        return false;
    }

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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (count == 0) {
            mAutoCompleteTextView.setCompoundDrawablesRelative(null, null, null, null);
        } else {
            Drawable drawable = getResources().getDrawable(android.R.drawable.ic_delete);
            <span style="color:#ff0000;">// 在 setCompoundDrawablesRelative 之前一定要对 Drawable 进行设置,不然没有效果</span>
            if (drawable != null) {
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            }
            mAutoCompleteTextView.setCompoundDrawablesRelative(null, null, drawable, null);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值