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

简介:

 

正文

  一、实现效果
 

    

       

 

  二、实现代码 
 

    监听输入

     /**
     * 动态搜索
     
*/
    
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. 维持清空操作后软键盘状态。

 


 

  三、小图标下载

      

    (右键另存为即可。)

 

结束 
 

  活用好每一个控件的属性、方法和事件能实现很多有意思的效果。欢迎大家交流。



本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/581678,如需转载请自行联系原作者

相关文章
|
移动开发 JavaScript 小程序
uView ActionSheet 操作菜单
uView ActionSheet 操作菜单
514 1
|
SQL Java 数据库
Spring Authorization Server 1.1 扩展实现 OAuth2 密码模式与 Spring Cloud 的整合实战(上)
Spring Authorization Server 1.1 扩展实现 OAuth2 密码模式与 Spring Cloud 的整合实战(上)
uni-app监听页面滚动
uni-app监听页面滚动
1056 0
|
7月前
|
存储 监控 前端开发
如何开发项目管理系统中的合同管理板块?(附架构图+流程图+代码参考)
合同管理是项目管理系统中的核心模块,涵盖合同生命周期、审批流程、履行监控及变更处理。通过数字化、自动化手段,提升管理效率,降低风险,确保合规性。本文详解合同管理模块的设计与开发,包括功能实现、业务流程及技术应用,助力企业构建高效管理系统。
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第10天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django以功能全面、文档完善著称,适合快速开发;Flask轻量灵活,易于上手;Pyramid介于两者之间,兼顾灵活性和安全性。选择框架时需考虑项目需求和个人偏好。
339 1
|
SQL 安全 关系型数据库
MySQL UDF提权
通过这些内容的详细介绍和实际案例分析,希望能帮助您深入理解MySQL UDF提权的机制、实现步骤及防范措施,提高系统的安全性和防护能力。
831 11
|
机器学习/深度学习 人工智能 编解码
阿里云GPU云服务器优惠收费标准,GPU服务器优缺点与适用场景详解
随着人工智能、大数据分析和高性能计算的发展,对计算资源的需求不断增加。GPU凭借强大的并行计算能力和高效的浮点运算性能,逐渐成为处理复杂计算任务的首选工具。阿里云提供了从入门级到旗舰级的多种GPU服务器,涵盖GN5、GN6、GN7、GN8和GN9系列,分别适用于图形渲染、视频编码、深度学习推理、训练和高性能计算等场景。本文详细介绍各系列的规格、价格和适用场景,帮助用户根据实际需求选择最合适的GPU实例。
|
自动驾驶 安全 物联网
|
存储 开发工具 数据安全/隐私保护
解决Gitee或者Github出现Access denied fatal: unable to access,The requested URL returned error: 403
解决Gitee或者Github出现Access denied fatal: unable to access,The requested URL returned error: 403
6271 0
小狼毫Rime输入法简单配置指南
小狼毫Rime输入法简单配置指南
3218 4