android中的编辑框EditText每输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢?

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


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

先新建布局文件

[html]  view plain copy
  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.     <ScrollView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent" >  
  10.   
  11.         <LinearLayout  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="fill_parent"  
  14.             android:orientation="vertical" >  
  15.   
  16.               
  17.             <EditText  
  18.                 android:id="@+id/id_edittext_1"  
  19.                 android:layout_width="fill_parent"  
  20.                 android:layout_height="wrap_content"   
  21.                 android:background="@drawable/alert_light"  
  22.                 android:textSize="10sp"  
  23.                 android:textColor="#ffff"/>  
  24.               
  25.             <TextView   
  26.                 android:id="@+id/id_textview"  
  27.                 android:layout_width="fill_parent"  
  28.                 android:layout_height="wrap_content"   
  29.                 android:textColor="#ffff"/>  
  30.               
  31.             <TextView   
  32.                 android:id="@+id/id_textview_1"  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="wrap_content"   
  35.                 android:background="@drawable/hah"  
  36.                 android:textColor="#f000"/>  
  37.               
  38.             <TextView   
  39.                 android:id="@+id/id_textview_2"  
  40.                 android:layout_width="fill_parent"  
  41.                 android:layout_height="wrap_content"   
  42.                 android:background="@drawable/hah"  
  43.                 android:textColor="#f000"  />   
  44.         LinearLayout>  
  45.     ScrollView>  
  46. LinearLayout>  
然后在代码中对编辑框绑定输入监听事件: [java]  view plain copy
  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.         setMyLayout();  
  16.     }  
  17.       
  18.     /** 
  19.      * 设置当前页面的布局 
  20.      */  
  21.     private void setMyLayout(){  
  22.         /*取得文本*/  
  23.         text_ = (TextView)findViewById(R.id.id_textview);  
  24.         text1_ = (TextView)findViewById(R.id.id_textview_1);  
  25.         text2_ = (TextView)findViewById(R.id.id_textview_2);       
  26.         /*取得编辑框*/  
  27.         edit1_ = (EditText)findViewById(R.id.id_edittext_1);  
  28.         /*监听 编辑框中的文本改变事件*/  
  29.         edit1_.addTextChangedListener(new TextWatcher() {  
  30.               
  31.             @Override  
  32.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  33.                 /*++ 文本每次改变就会跑这个方法 ++*///这里count 是 当前输入字符个数
  34.                 if(null != text_){  
  35.                     text_.setText("您正在输入......\n当前光标处在第 " + start  
  36.                             +" 个位置\n您选择处理了 " + before + " 个字符\n您这次输入的词语有 "  
  37.                             + count + " 个字符");  
  38.                 }        
  39.             }  
  40.               
  41.             @Override  
  42.             public void beforeTextChanged(CharSequence s, int start, int count,  
  43.                             int after) {  
  44.                 /*++这里的count树枝上是和onTextChanged()里的before一样的 
  45.                  * after树枝上是和onTextChanged()里的count一样的 ++*/  
  46.                 if(null != text1_){  
  47.                     text1_.setText("您正在输入......\n当前光标处在第 " + start  
  48.                             +" 个位置\n您选择处理了 " + count + " 个字符\n您这次输入的词语有 "  
  49.                             + after + " 个字符");  
  50.                 }  
  51.             }  
  52.               
  53.             @Override  
  54.             public void afterTextChanged(Editable s) {  
  55.                 /*++这里显示出输入的字符串++*/  
  56.                 if(null != text2_){  
  57.                     text2_.setText(s);  
  58.                 }  
  59.             }        
  60.     });  
  61.     }  
  62. }  

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

转载自:http://blog.csdn.net/zoeice/article/details/7700529


更多相关文章

  1. android Field类的讲解
  2. Android之解决多语言适配部分TextView内容左对齐和内容一行不排
  3. Android(安卓)开发学习笔记
  4. Kotlin学习笔记(一)---从零学习Kotlin
  5. pinyin4j在Android中的使用
  6. Android(安卓)混淆打包
  7. Eclipse下载Github用Android(安卓)Studio编辑的Android源码
  8. android studio wifi连接真机调试
  9. Android(安卓)查看pdf文档——PDFView

随机推荐

  1. ToggleButton的使用
  2. [Android] 单独编译生成boot.img时mkboot
  3. android导入工程报错
  4. Android tab 学习
  5. Android——RatingBar(评价条)相关知识总结
  6. sensor
  7. Android选项卡TabHost方式实现
  8. android ActionBar(官方指导)
  9. (原)Eclipse的java中文件读写
  10. ListView小知识整理:滑动背景、Item间隙等