监听EditView中的文本改变事件详解--转

转自:

http://blog.csdn.net/zoeice/article/details/7700529

android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢?

我们可以建一个例子,效果图如下:

我们可以监听光标处在哪个位置,选择了几个字符并处理,输入了几个字符

先新建布局文件

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"   
  5.     android:background="@drawable/af">  
  6.   
  7.     <!-- 上下滚动 -->  
  8.   
  9.     <ScrollView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:orientation="vertical" >  
  17.   
  18.             <!-- 编辑框 -->  
  19.   
  20.             <EditText  
  21.                 android:id="@+id/id_edittext_1"  
  22.                 android:layout_width="fill_parent"  
  23.                 android:layout_height="wrap_content"   
  24.                 android:background="@drawable/alert_light"  
  25.                 android:textSize="10sp"  
  26.                 android:textColor="#ffff"  
  27.                 />  
  28.               
  29.             <TextView   
  30.                 android:id="@+id/id_textview"  
  31.                 android:layout_width="fill_parent"  
  32.                 android:layout_height="wrap_content"   
  33.                 android:textColor="#ffff"  
  34.                 />  
  35.               
  36.             <TextView   
  37.                 android:id="@+id/id_textview_1"  
  38.                 android:layout_width="fill_parent"  
  39.                 android:layout_height="wrap_content"   
  40.                 android:background="@drawable/hah"  
  41.                 android:textColor="#f000"  
  42.                 />  
  43.               
  44.             <TextView   
  45.                 android:id="@+id/id_textview_2"  
  46.                 android:layout_width="fill_parent"  
  47.                 android:layout_height="wrap_content"   
  48.                 android:background="@drawable/hah"  
  49.                 android:textColor="#f000"  
  50.                 />  
  51.               
  52.         </LinearLayout>  
  53.     </ScrollView>  
  54.   
  55. </LinearLayout>  

然后在代码中对编辑框绑定输入监听事件:

[java] view plaincopy
 
  1. public class EditTextTestActivity extends Activity {  
  2.     /**编辑框*/  
  3.     private EditText edit1_;  
  4.     /**文本*/  
  5.     private TextView text_;  
  6.     private TextView text1_;  
  7.     private TextView text2_;  
  8.       
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.           
  15.         /*设置当前页面的布局*/  
  16.         setMyLayout();  
  17.     }  
  18.       
  19.     /** 
  20.      * 设置当前页面的布局 
  21.      */  
  22.     private void setMyLayout(){  
  23.         /*取得文本*/  
  24.         text_ = (TextView)findViewById(R.id.id_textview);  
  25.         text1_ = (TextView)findViewById(R.id.id_textview_1);  
  26.         text2_ = (TextView)findViewById(R.id.id_textview_2);  
  27.           
  28.         /*取得编辑框*/  
  29.         edit1_ = (EditText)findViewById(R.id.id_edittext_1);  
  30.         /*监听 编辑框中的文本改变事件*/  
  31.         edit1_.addTextChangedListener(new TextWatcher() {  
  32.               
  33.             @Override  
  34.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  35.                 /*++ 文本每次改变就会跑这个方法 ++*/  
  36.                 if(null != text_){  
  37.                     text_.setText("您正在输入...... 当前光标处在第 " + start  
  38.                             +" 个位置 您选择处理了 " + before + " 个字符 您这次输入的词语有 "  
  39.                             + count + " 个字符");  
  40.                 }  
  41.                   
  42.             }  
  43.               
  44.             @Override  
  45.             public void beforeTextChanged(CharSequence s, int start, int count,  
  46.                             int after) {  
  47.                 /*++这里的count树枝上是和onTextChanged()里的before一样的 
  48.                  * after树枝上是和onTextChanged()里的count一样的 ++*/  
  49.                 if(null != text1_){  
  50.                     text1_.setText("您正在输入...... 当前光标处在第 " + start  
  51.                             +" 个位置 您选择处理了 " + count + " 个字符 您这次输入的词语有 "  
  52.                             + after + " 个字符");  
  53.                 }  
  54.             }  
  55.               
  56.             @Override  
  57.             public void afterTextChanged(Editable s) {  
  58.                 /*++这里显示出输入的字符串++*/  
  59.                 if(null != text2_){  
  60.                     text2_.setText(s);  
  61.                 }  
  62.             }  
  63.               
  64.     });  
  65.   
  66.     }  
  67. }  

然后就ok了,很多地都可以用到这个办法。

源代码在下面:

http://download.csdn.net/detail/zoeice/4399601

原文地址:https://www.cnblogs.com/mochaMM/p/5150909.html