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的变化的更多相关文章

  1. Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》

    昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下.真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况. 监听文本输入情况,仅仅限于土司略显low ...

  2. 使用TextWatcher监听EditText变化

    public class MainActivity extends AppCompatActivity { private TextView mTextView; private EditText m ...

  3. listview监听组件内容变化

    package com.meizu.ui.gifts; import android.app.Activity; import android.content.Context; import andr ...

  4. HTML5 oninput实时监听输入框值变化的完美方案

    在网页开发中经常会碰到需要动态监听输入框值变化的情况,如果使用 onkeydown.onkeypress.onkeyup 这个几个键盘事件来监测的话,监听不了右键的复制.剪贴和粘贴这些操作,处理组合快 ...

  5. 【转载】实时监听输入框值变化的完美方案:oninput & onpropertychange

    oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有 ...

  6. 实时监听输入框值变化:oninput & onpropertychange

    结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. oninput 是 HTML5 的标准事件,对于检测 textarea, i ...

  7. js/jquery 实时监听输入框值变化的完美方案:oninput & onpropertychange

    (1)     先说jquery, 使用 jQuery 库的话,只需要同时绑定 oninput 和 onpropertychange 两个事件就可以了,示例代码: $('#username').bin ...

  8. JS 获取和监听屏幕方向变化(portrait / landscape)

    移动设备的屏幕有两个方向: landscape(横屏)和portrait(竖屏),在某些情况下需要获取设备的屏幕方向和监听屏幕方向的变化,因此可以使用Javascript提供的 MediaQueryL ...

  9. 实时监听输入框值变化的完美方案:oninput & onpropertychange

    实时监听输入框值变化的完美方案:oninput & onpropertychange: 网址:http://www.cnblogs.com/lhb25/archive/2012/11/30/o ...

随机推荐

  1. MySQL源码之mysqld启动

    启动mysqld,并进入listen阶段   函数调用栈: mysqld_main():        my_init();初始化变量,锁,错误串      my_thread_global_init ...

  2. Unity3D之Character Controller(CC)与GameObject的碰撞方法

    先来一部分网上常见的内容(略整理): --------------------分隔线---------------------- Unity3d中参与碰撞的物体分2种类型: 一.发起碰撞的物体. 二. ...

  3. 彻底卸载oracle10g

    如果Oracle安装在Windows上,那么删除起来特别麻烦,以下列出具体步骤: 软件环境: Windows 7.ORACLE 10.1.24:ORACLE安装路径为:C:/ORACLE 实现方法: ...

  4. DevExpress控件汉化类 z

    更新了一些字段,VER9.3.3 using System; using DevExpress.XtraEditors.Controls; using DevExpress.XtraGrid.Loca ...

  5. ARM学习笔记2——分支跳转指令

    一.Arm指令条件码和条件助记符 二.跳转指令B 1.作用 跳转指令B使程序跳转到指定的地址执行程序(跳转范围是PC-32MB到PC+32MB) 2.指令格式(注:B后面如果有条件,条件就是紧跟在B后 ...

  6. 奇怪的Lisp和难懂的计算机程序的构造和解释

    最近用新买的 Kindle 看<黑客与画家>的Lisp部分,发现作者 Paul Graham 很推崇 Lisp 语言,并且认为其它语言都没有Lisp简洁“成熟”,并且举例证明其它语言都在往 ...

  7. CentOS环境下yum安装LAMP(Linux+Apache+Mysql+php)

    CentOS下使用yum命令 安装LAMP详细过程.我们使用的软件是CentOS的最新版本CentOS 6.3,其他版本的也基本类似. 第一步:更新系统内核(如果不想更新可以跳过本步). 首先更新系统 ...

  8. Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜

    1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 122  So ...

  9. 【转】shell 教程——04 什么时候使用Shell

    因为Shell似乎是各UNIX系统之间通用的功能,并且经过了POSIX的标准化.因此,Shell脚本只要“用心写”一次,即可应用到很多系统上.因此,之所以要使用Shell脚本是基于: 简单性:Shel ...

  10. HDU4283:You Are the One(区间DP)

    Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...