监听EditText的变化
http://liangruijun.blog.51cto.com/3061169/729505
之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下EditText。
监听EditText的变化
使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法:

当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。
MainActivity.java
package com.lingdududu.watcher;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText text;
String str;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText)findViewById(R.id.text);
text.addTextChangedListener(textWatcher);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.d("TAG","afterTextChanged--------------->");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
Log.d("TAG","beforeTextChanged--------------->");
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.d("TAG","onTextChanged--------------->");
str = text.getText().toString();
try {
//if ((heighText.getText().toString())!=null)
Integer.parseInt(str);
} catch (Exception e) {
// TODO: handle exception
showDialog();
}
}
};
private void showDialog(){
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);
builder.setMessage("你输出的整型数字有误,请改正");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = builder.create();
dialog.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入整型数字"
/>
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
效果图:
当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正

在LogCat查看调用这些方法的顺序:
beforeTextChanged-->onTextChanged-->onTextChanged

第二个例子实现了提示文本框还能输入多少个字符的功能
package com.lingdududu.test;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button clearBtn;
private EditText et;
private TextView tv;
final int MAX_LENGTH = 20;
int Rest_Length = MAX_LENGTH;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv =(TextView)findViewById(R.id.tv);
et = (EditText)findViewById(R.id.et);
clearBtn = (Button)findViewById(R.id.btn);
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
tv.setText("还能输入"+Rest_Length+"个字");
}
@Override
public void afterTextChanged(Editable s) {
tv.setText("还能输入"+Rest_Length+"个字");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(Rest_Length>0){
Rest_Length = MAX_LENGTH - et.getText().length();
}
}
});
clearBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
et.setText("");
Rest_Length = MAX_LENGTH;
}
});
}
}
效果图:

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/729505
监听EditText的变化的更多相关文章
- Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下.真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况. 监听文本输入情况,仅仅限于土司略显low ...
- 使用TextWatcher监听EditText变化
public class MainActivity extends AppCompatActivity { private TextView mTextView; private EditText m ...
- listview监听组件内容变化
package com.meizu.ui.gifts; import android.app.Activity; import android.content.Context; import andr ...
- HTML5 oninput实时监听输入框值变化的完美方案
在网页开发中经常会碰到需要动态监听输入框值变化的情况,如果使用 onkeydown.onkeypress.onkeyup 这个几个键盘事件来监测的话,监听不了右键的复制.剪贴和粘贴这些操作,处理组合快 ...
- 【转载】实时监听输入框值变化的完美方案:oninput & onpropertychange
oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有 ...
- 实时监听输入框值变化:oninput & onpropertychange
结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. oninput 是 HTML5 的标准事件,对于检测 textarea, i ...
- js/jquery 实时监听输入框值变化的完美方案:oninput & onpropertychange
(1) 先说jquery, 使用 jQuery 库的话,只需要同时绑定 oninput 和 onpropertychange 两个事件就可以了,示例代码: $('#username').bin ...
- JS 获取和监听屏幕方向变化(portrait / landscape)
移动设备的屏幕有两个方向: landscape(横屏)和portrait(竖屏),在某些情况下需要获取设备的屏幕方向和监听屏幕方向的变化,因此可以使用Javascript提供的 MediaQueryL ...
- 实时监听输入框值变化的完美方案:oninput & onpropertychange
实时监听输入框值变化的完美方案:oninput & onpropertychange: 网址:http://www.cnblogs.com/lhb25/archive/2012/11/30/o ...
随机推荐
- Ext.onReady(function(){} )函数的作用域分析(1)
Ext.onReady(function(){ var genResultDelete = function(){ alert('delete') ; } var renderResult = fun ...
- Beta Round #9 (酱油杯noi考后欢乐赛)乌鸦喝水
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- Unity3D插件之Easy Touch 3.1(1): Easy Joystick
先看官方介绍:https://www.assetstore.unity3d.com/#/content/3322 (Allows you to quickly and easily develop a ...
- 如何避免jQuery库和其他库的冲突
默认情形:jQuery用$作为自身的快捷方式 1. jQuery库在其他库之后导入 (1)方法:使用jQuery.noConflict()函数将变量$的控制权转移给其他库 (2)操作: (a)在js代 ...
- Ubuntu下安装PAC Manager
在Windows下习惯使用XShell.PAC Manager其实就是一个在Linux系统类似XShell的工具. 下载地址: https://sourceforge.net/projects/pac ...
- java的好资料总结
1jvm的垃圾回收http://wenku.baidu.com/link?url=gf08pYxNxVC2ZR607Qv9gn1pkFs5T1Pp5YHxISBEFdcz0D1HdK-7YOuSDft ...
- 在线App开发平台——应用之星傻瓜式开发平台
随着智能手机及APP应用程序的普及,越来越多的企业和个人意识到APP的营销价值,出于对技术的敬畏,很多企业下意识认为开发APP是一个有难度的技术活,所以很多时候有心无力,也担心APP的后续的技术支持. ...
- CentOS 7下安装xampp和testlink
记录一下最近安装testlink的经历,供大伙儿参考,有问题可以留言讨论,这里就不截图了 先说下安装版本: CentOS-7.0-1406-x86_64-DVDxampp-linux-1.8.3-5- ...
- linux下网络配置 命令
一.IP的配置: 不直接修改文件方式: 设置网卡eth0的IP地址和子网掩码: sudo ifconfig eth0 192.168.2.1 netmask 255.255.255.0 将IP地址改为 ...
- 【iOS 7】使用UIScreenEdgePanGestureRecognizer实现swipe to pop效果
在iOS 7还没有发布的时候,各种App实现各种的swipe to pop效果,比如这里有一份简单的demo. 在iOS 7上,只要是符合系统的导航结构: - (BOOL)application:(U ...