当前位置: 首页 > news >正文

Android文本框实现搜索和清空效果

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

本文实现的效果:

文本框输入为空时显示输入的图标;不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字。

实现效果:

103233_L6ux_1251149.jpg

103233_cAuI_1251149.jpg

核心代码:

package com.example.test;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

public class SearchActivity extends Activity {

    private Drawable mIconSearchDefault; // 搜索文本框默认图标
    private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标
    private EditText mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Resources res = getResources();
        //搜索图标
        mIconSearchDefault = res.getDrawable(R.drawable.txt_search_default);
        //清除图标
        mIconSearchClear = res.getDrawable(R.drawable.txt_search_clear);

        mSearchView = (EditText) findViewById(R.id.edt_search);
        mSearchView.addTextChangedListener(tbxSearch_TextChanged);
        mSearchView.setOnTouchListener(txtSearch_OnTouch);
        //设置默认图标
        mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                null, mIconSearchDefault, null);
    }

    /**
     * 动态搜索
     */
    private TextWatcher tbxSearch_TextChanged = new TextWatcher() {

        // 缓存上一次文本框内是否为空
        private boolean isnull = true;

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s)) {
                if (!isnull) {
                    mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                            null, mIconSearchDefault, null);
                    isnull = true;
                }
            } else {
                if (isnull) {
                    mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                            null, mIconSearchClear, null);
                    isnull = 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) {
            //查询搜索
        }
    };
    /** 点击清除图标,清空文本框*/
    private OnTouchListener txtSearch_OnTouch = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                int curX = (int) event.getX();
                //判断触摸位置在右端,并且文本框内有内容
                if (curX > v.getWidth() - 88
                        && !TextUtils.isEmpty(mSearchView.getText())) {
                    mSearchView.setText("");
                    int cacheInputType = mSearchView.getInputType();// backup the input type
                    mSearchView.setInputType(InputType.TYPE_NULL);// disable soft input
                    mSearchView.onTouchEvent(event);// call native handler
                    mSearchView.setInputType(cacheInputType);// restore input type
                    return true;// consume touch even
                }
                break;
            }
            return false;
        }
    };
}

    代码说明:

      1. 为输入框绑定触摸事件(模拟点击事件捕捉)。通过监听点击区域判断是否点击清空图片,

                        如果在该区域并且文本框不为空,则清空文本框。

      2. 为输入框绑定文本改变事件监听,根据内容改变动态设置图标显示。

      3. 维持清空操作后软键盘状态。


转载于:https://my.oschina.net/u/1251149/blog/200695

相关文章:

  • strongweak
  • powershell最常用的命令之(一)
  • 左固定右边自适应框架
  • logrotate工具的使用
  • ping,
  • php操作mysql数据库类代码
  • 恶补英语-1
  • 随机选择实现
  • 重读金典------高质量C编程指南(林锐)-------第六章 函数设计
  • Oracle 修改表列属性
  • CKEditor如何统计文字数量
  • Oracle 11G创建表空间、用户、授权命令(Oracle 11g使用)
  • TIMESTAMP with ****问题连不上mysql
  • window.location.href的一种用法
  • StringBuffer 用法
  • [NodeJS] 关于Buffer
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • const let
  • extract-text-webpack-plugin用法
  • in typeof instanceof ===这些运算符有什么作用
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • mysql_config not found
  • MySQL数据库运维之数据恢复
  • PAT A1120
  • PHP的Ev教程三(Periodic watcher)
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 简析gRPC client 连接管理
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 前言-如何学习区块链
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 我是如何设计 Upload 上传组件的
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 栈实现走出迷宫(C++)
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • #{}和${}的区别是什么 -- java面试
  • #QT项目实战(天气预报)
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • #图像处理
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (0)Nginx 功能特性
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (安卓)跳转应用市场APP详情页的方式
  • (八)Flask之app.route装饰器函数的参数
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • (转) RFS+AutoItLibrary测试web对话框