使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听

本文主要是介绍使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下EditText。

监听EditText的变化

使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法:

 当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。

MainActivity.java

 
  1. package com.lingdududu.watcher;  
  2.  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.text.Editable;  
  8. import android.text.TextWatcher;  
  9. import android.util.Log;  
  10. import android.widget.EditText;  
  11.  
  12. public class MainActivity extends Activity {  
  13.     private EditText text;  
  14.     String str;  
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         text = (EditText)findViewById(R.id.text);  
  21.         text.addTextChangedListener(textWatcher);  
  22.     }  
  23.       
  24.     private TextWatcher textWatcher = new TextWatcher() {  
  25.           
  26.         @Override    
  27.         public void afterTextChanged(Editable s) {     
  28.             // TODO Auto-generated method stub     
  29.             Log.d("TAG","afterTextChanged--------------->");   
  30.         }   
  31.           
  32.         @Override 
  33.         public void beforeTextChanged(CharSequence s, int start, int count,  
  34.                 int after) {  
  35.             // TODO Auto-generated method stub  
  36.             Log.d("TAG","beforeTextChanged--------------->");  
  37.         }  
  38.  
  39.          @Override    
  40.         public void onTextChanged(CharSequence s, int start, int before,     
  41.                 int count) {     
  42.             Log.d("TAG","onTextChanged--------------->");    
  43.             str = text.getText().toString();  
  44.             try {  
  45.                 //if ((heighText.getText().toString())!=null)   
  46.                 Integer.parseInt(str);  
  47.                   
  48.             } catch (Exception e) {  
  49.                 // TODO: handle exception  
  50.                 showDialog();  
  51.             }  
  52.                               
  53.         }                    
  54.     };  
  55.  
  56.     private void showDialog(){  
  57.         AlertDialog dialog;  
  58.         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
  59.         builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);  
  60.         builder.setMessage("你输出的整型数字有误,请改正");  
  61.         builder.setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  62.             @Override 
  63.             public void onClick(DialogInterface dialog, int which) {  
  64.                 // TODO Auto-generated method stub  
  65.                   
  66.             }                     
  67.         });  
  68.         dialog = builder.create();  
  69.         dialog.show();  
  70.     }  

 main.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="请输入整型数字" 
  11.     /> 
  12. <EditText   
  13.     android:id="@+id/text" 
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     /> 
  17. </LinearLayout> 

  效果图:

当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正

在LogCat查看调用这些方法的顺序:

beforeTextChanged-->onTextChanged-->onTextChanged

第二个例子实现了提示文本框还能输入多少个字符的功能

 
  1. package com.lingdududu.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.  
  12. public class MainActivity extends Activity {  
  13.  private Button clearBtn;  
  14.  private EditText et;  
  15.  private TextView tv;  
  16.  final int MAX_LENGTH = 20;  
  17.  int Rest_Length = MAX_LENGTH;  
  18.     @Override 
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         tv =(TextView)findViewById(R.id.tv);  
  23.         et = (EditText)findViewById(R.id.et);  
  24.           
  25.         clearBtn = (Button)findViewById(R.id.btn);  
  26.  
  27.         et.addTextChangedListener(new TextWatcher() {  
  28.                           
  29.             @Override 
  30.             public void beforeTextChanged(CharSequence s, int start, int count,  
  31.                     int after) {  
  32.                 tv.setText("还能输入"+Rest_Length+"个字");              
  33.             }  
  34.                           
  35.             @Override 
  36.             public void afterTextChanged(Editable s) {  
  37.                 tv.setText("还能输入"+Rest_Length+"个字");  
  38.             }  
  39.               
  40.             @Override 
  41.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  42.                 if(Rest_Length>0){  
  43.                     Rest_Length = MAX_LENGTH - et.getText().length();  
  44.                 }  
  45.             }             
  46.         });  
  47.         
  48.         clearBtn.setOnClickListener(new Button.OnClickListener() {        
  49.             @Override 
  50.             public void onClick(View v) {  
  51.                 et.setText("");  
  52.                 Rest_Length = MAX_LENGTH;  
  53.             }  
  54.         });  
  55.     }  
  56.  } 

效果图:

 

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/729505

这篇关于使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/982010

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当