监听EditText的变化

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

http://liangruijun.blog.51cto.com/3061169/729505


使用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> 

 效果图:

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值