前言

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

声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com

    农民伯伯: http://over140.cnblogs.com

正文

  一、实现效果

    

       

  二、实现代码

    监听输入


    /**
     * 动态搜索
     */
    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() - 38
                        && !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;
        }
    };

    绑定事件


    private Drawable mIconSearchDefault; // 搜索文本框默认图标
    private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.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.txtSearch);
        mSearchView.addTextChangedListener(tbxSearch_TextChanged);
        mSearchView.setOnTouchListener(txtSearch_OnTouch);
    }

    代码说明:

      1. 为输入框绑定触摸事件(模拟点击事件捕捉)。通过监听点击区域判断是否点击清空图片,如果在该区域并且文本框不为空,则清空文本框。

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

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

  三、参考

    1.  how to block virtual keyboard while clicking on edittext in android?

  四、小图标下载

      

    (右键另存为即可。)

感谢农民伯伯的文章

Android EditText 文本框实现搜索和清空效果的更多相关文章

  1. android自定义文本框,后面带清空按钮

    android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...

  2. android 弹出的软键盘遮挡住EditText文本框的解决方案

    1.android 弹出的软键盘遮挡住EditText文本框的解决方案: 把Activit对应的布局文件filename.xml文件里的控件用比重设置布局.(例如:android:layout_wei ...

  3. Xamarin Android自定义文本框

    xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(m ...

  4. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  5. Android 富文本框实现 RichEditText

    Android系统自带控件没有富文本框控件,如果想写一封带格式的邮件基本上不可能,EdtiText只有默认一种格式,显示不能滿足要求,!!正好项目需要研究了一下,开发了此控件,现将一些源代码开放一下, ...

  6. 使用 jquery-autocomplete插件 完成文本框输入自动填充联想效果 解决兼容IE输入中文问题

    项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...

  7. Android点击EditText文本框之外任何地方隐藏键盘的解决办法

    1,实现方法一:通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),在事件里进行键盘隐藏 <LinearLayout xmlns:android=&q ...

  8. android 获取文本框回车输入

    扫描头开启,并发送回车 txtUsername.setOnEditorActionListener(new OnEditorActionListener() { @Override public bo ...

  9. Android EditText文本内容变化监听方法

    package com.google; import android.app.Activity; import android.os.Bundle; import android.text.Edita ...

随机推荐

  1. powerdesigner 绘制表关系和导出sql

    1.生成图(A图,B图) 2.创建关系(palette工具栏里有个线条,主表子表关系连接即可,拖动是由顺序的,一对多即从A表往B表连接) 3.这里小说一下   一开始是CDM模式,可以在软件最顶层看到 ...

  2. 关于 hangfire 初始化工作机制

    hangfire初始化的配置方法 :UseHangfire . public static class OwinBootstrapper { /// <summary> /// Boots ...

  3. angular(常识)

    我觉得angularjs是前端框架,而jquery只是前端工具,这两个还是没有可比性的. 看知乎上关于jquery和angular的对比http://www.zhihu.com/question/27 ...

  4. JAVA并发的性能调整

    1.互斥技术 synchronized Lock Atomic 性能比较Atomic >  Lock  > synchronized,当然这不是绝对的.当线程数比较少时,synchroni ...

  5. Spring 事务配置管理,简单易懂,详细 [声明式]

    Spring 事务配置说明 Spring 如果没有特殊说明,一般指是跟数据存储有关的数据操作事务操作:对于数据持久操作的事务配置,一般有三个对象,数据源,事务管理器,以及事务代理机制: Spring ...

  6. python:open文件操作

    file: jim|123|1 tom|321|3 kamil|432|1 # __author__ = liukun # coding:utf-8 obj = open('file.txt','r' ...

  7. Mysql常出现的问题

    1.mysql如何导入.txt文件?load data local infile 'D:\\data.txt' into table 表名 fields terminated by '\t';2.my ...

  8. 搭建一个springMVC项目以及遇到的问题

    首先找到jar包(lz现在还在学习maven,以后回了,就用maven了,自己配置时,jar包不全就很容易到时搭建失败)

  9. 【poj3233】 Matrix Power Series

    http://poj.org/problem?id=3233 (题目链接) 题意 给出一个n×n的矩阵A,求模m下A+A2+A3+…+Ak 的值 Solution 今日考试就A了这一道题.. 当k为偶 ...

  10. java中的不为空判断

    String不为空判断 if(null != str && !"".equals(str)) List不为空判断 if(list!=null && ...