首页 > 技术文章 > 【Android进阶学习】监听EditText的变化

huanpeng 2015-03-10 09:40 原文

之前博客上的有关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         }  
71     } 

-----------------------------------------------------------------------------------------------------

 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

 

推荐阅读