Android搜索视图清除文本按钮监听器
创始人
2024-10-10 18:31:46
0

以下是一个示例代码,展示了如何在Android搜索视图中添加一个清除文本按钮的监听器:

import android.content.Context;
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 android.widget.EditText;

public class ClearableEditText extends EditText {

    // 清除按钮的图标
    private Drawable clearButton;

    public ClearableEditText(Context context) {
        super(context);
        init();
    }

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // 获取清除按钮的图标
        clearButton = getResources().getDrawable(android.R.drawable.ic_menu_close_clear_cancel);

        // 设置清除按钮的位置
        setCompoundDrawablesWithIntrinsicBounds(null, null, clearButton, null);

        // 添加文本改变的监听器
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 根据文本的长度显示或隐藏清除按钮
                setClearButtonVisible(s.length() > 0);
            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        // 添加触摸事件监听器,点击清除按钮时清空文本
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (getCompoundDrawables()[2] != null) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (event.getX() > getWidth() - getPaddingRight() - clearButton.getIntrinsicWidth()) {
                            setText("");
                            return true;
                        }
                    }
                }
                return false;
            }
        });
    }

    // 设置清除按钮的可见性
    private void setClearButtonVisible(final boolean visible) {
        setCompoundDrawablesWithIntrinsicBounds(null, null, visible ? clearButton : null, null);
    }
}

使用这个ClearableEditText自定义控件替换你的搜索视图中的EditText,它会添加一个清除文本按钮的监听器,并在文本改变时显示或隐藏清除按钮。点击清除按钮会清空文本。

相关内容

热门资讯

透视系统"福建大菠萝... 1、透视系统"福建大菠萝万能辅助器"详细辅助科技教程(可以挂底牌透明)。2、福建大菠萝万能辅助器透视...
透视有挂"wepok... 1、透视有挂"wepoker辅助器激活码"详细辅助2025版教程(底牌透视神器)。2、wepoker...
透视ai代打"wep... 1、透视ai代打"wepoker高级辅助"详细辅助实用技巧(高级辅助神器)(UU poker、wep...
透视黑科技"hhpo... 透视黑科技"hhpoker软件可以玩吗"详细辅助插件教程(私人局外卦软件)1、许多玩家不知道hhpo...
透视能赢"哈糖大菠萝... 透视能赢"哈糖大菠萝万能挂"详细辅助玩家教你(辅助器v3.3教程);原来确实真的有挂(需添加指定薇1...
透视好友"hhpok... 透视好友"hhpoker作弊码"详细辅助黑科技教程(轻量版辅助软件)是一款可以让一直输的玩家,快速成...
透视中牌率"哈糖大菠... 透视中牌率"哈糖大菠萝有挂吗5个常用方法"详细辅助透牌教程(开挂技巧)是由北京得哈糖大菠萝有挂吗5个...
透视私人局"wepo... 透视私人局"wepokerplus透视脚本免费"详细辅助安装教程(辅助器激活码辅助);原来确实真的有...
透视苹果版"hhpo... 1、透视苹果版"hhpoker免费透视脚本"详细辅助力荐教程(辅助插件功能神器)。2、hhpoker...
透视计算"wepok... 透视计算"wepoker能不能透视"详细辅助黑科技教程(透视辅助下载辅助);致您一封信;亲爱wepo...