【Android进阶学习】监听EditText的变化

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

监听EditText的变化

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

blob.png

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

MainActivity.java

  1. package com.lingdududu.watcher;  

  2. import android.app.Activity;  

  3. import android.app.AlertDialog;  

  4. import android.content.DialogInterface;  

  5. import android.os.Bundle;  

  6. import android.text.Editable;  

  7. import android.text.TextWatcher;  

  8. import android.util.Log;  

  9. import android.widget.EditText;  

  10. public class MainActivity extends Activity {  

  11.     private EditText text;  

  12.     String str;  

  13.     @Override 

  14.     public void onCreate(Bundle savedInstanceState) {  

  15.         super.onCreate(savedInstanceState);  

  16.         setContentView(R.layout.main);  

  17.         text = (EditText)findViewById(R.id.text);  

  18.         text.addTextChangedListener(textWatcher);  

  19.     }  

  20.     private TextWatcher textWatcher = new TextWatcher() {  

  21.         @Override    

  22.         public void afterTextChanged(Editable s) {     

  23.             // TODO Auto-generated method stub     

  24.             Log.d("TAG","afterTextChanged--------------->");   

  25.         }   

  26.         @Override 

  27.         public void beforeTextChanged(CharSequence s, int start, int count,  

  28.                 int after) {  

  29.             // TODO Auto-generated method stub  

  30.             Log.d("TAG","beforeTextChanged--------------->");  

  31.         }  

  32.          @Override    

  33.         public void onTextChanged(CharSequence s, int start, int before,     

  34.                 int count) {     

  35.             Log.d("TAG","onTextChanged--------------->");    

  36.             str = text.getText().toString();  

  37.             try {  

  38.                 //if ((heighText.getText().toString())!=null)   

  39.                 Integer.parseInt(str);  

  40.             } catch (Exception e) {  

  41.                 // TODO: handle exception  

  42.                 showDialog();  

  43.             }  

  44.         }                    

  45.     };  

  46.     private void showDialog(){  

  47.         AlertDialog dialog;  

  48.         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  

  49.         builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);  

  50.         builder.setMessage("你输出的整型数字有误,请改正");  

  51.         builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  

  52.             @Override 

  53.             public void onClick(DialogInterface dialog, int which) {  

  54.                 // TODO Auto-generated method stub  

  55.             }                     

  56.         });  

  57.         dialog = builder.create();  

  58.         dialog.show();  

  59.     }  

 main.xml

  1.  
  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> 

效果图:

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

blob.png

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

beforeTextChanged-->onTextChanged-->onTextChanged

blob.png

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

  1. package com.lingdududu.test;  

  2. import android.app.Activity;  

  3. import android.os.Bundle;  

  4. import android.text.Editable;  

  5. import android.text.TextWatcher;  

  6. import android.view.View;  

  7. import android.widget.Button;  

  8. import android.widget.EditText;  

  9. import android.widget.TextView;  

  10. public class MainActivity extends Activity {  

  11.  private Button clearBtn;  

  12.  private EditText et;  

  13.  private TextView tv;  

  14.  final int MAX_LENGTH = 20;  

  15.  int Rest_Length = MAX_LENGTH;  

  16.     @Override 

  17.     public void onCreate(Bundle savedInstanceState) {  

  18.         super.onCreate(savedInstanceState);  

  19.         setContentView(R.layout.main);  

  20.         tv =(TextView)findViewById(R.id.tv);  

  21.         et = (EditText)findViewById(R.id.et);  

  22.         clearBtn = (Button)findViewById(R.id.btn);  

  23.         et.addTextChangedListener(new TextWatcher() {  

  24.             @Override 

  25.             public void beforeTextChanged(CharSequence s, int start, int count,  

  26.                     int after) {  

  27.                 tv.setText("还能输入"+Rest_Length+"个字");              

  28.             }  

  29.             @Override 

  30.             public void afterTextChanged(Editable s) {  

  31.                 tv.setText("还能输入"+Rest_Length+"个字");  

  32.             }  

  33.             @Override 

  34.             public void onTextChanged(CharSequence s, int start, int before, int count) {  

  35.                 if(Rest_Length>0){  

  36.                     Rest_Length = MAX_LENGTH - et.getText().length();  

  37.                 }  

  38.             }             

  39.         });  

  40.         clearBtn.setOnClickListener(new Button.OnClickListener() {        

  41.             @Override 

  42.             public void onClick(View v) {  

  43.                 et.setText("");  

  44.                 Rest_Length = MAX_LENGTH;  

  45.             }  

  46.         });  

  47.     }  

  48.  } 

效果图:

blob.png

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