你好,游客 登录 注册 搜索
背景:
阅读新闻

Android软件开发之EditText 详解

[日期:2011-08-12] 来源:Linux社区  作者:雨松MOMO [字体:大 中 小]
5.监听软键盘的按键事件

        做项目的时候 有时候须要在用户输入内容时做检测,比如如果用户输入不合法的内容不予以显示在EditText中, 这时候我就要用到addTextChangedListener 用它来监听用户输入状态。可以在监听中改变用户输入的内容或者提示用户输入内容不合法等等。 如图所示我的每次输入操作都可以被正常的监听出来,用户输入内容的正常流程 beforeTextChanged()  -》onTextChanged()  -》afterTextChanged()然后是通知屏幕绘制 显示在屏幕上 所以我们可以在这三个方法中来修改用户输入内容 或者截取用户输入的内容。 




 
  1. package cn.m15.xys;  
  2.   
  3. import Android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.   
  10. public class MonitorKeyActivity extends Activity {  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.     setContentView(R.layout.monitorkey);  
  14.     EditText editText = (EditText)findViewById(R.id.monitor_edit_text0);  
  15.     final TextView textView0 = (TextView)findViewById(R.id.monitor_text0);  
  16.     final TextView textView1 = (TextView)findViewById(R.id.monitor_text1);  
  17.     final TextView textView2 = (TextView)findViewById(R.id.monitor_text2);  
  18.       
  19.     editText.addTextChangedListener(new TextWatcher() {  
  20.           
  21.         @Override  
  22.         public void onTextChanged(CharSequence text, int start, int before, int count) {  
  23.                 //text  输入框中改变后的字符串信息   
  24.         //start 输入框中改变后的字符串的起始位置   
  25.         //before 输入框中改变前的字符串的位置 默认为0   
  26.         //count 输入框中改变后的一共输入字符串的数量   
  27.         textView1.setText("输入后字符串 [ " + text.toString() + " ] 起始光标 [ " + start + " ] 输入数量 [ " + count+" ]");  
  28.           
  29.         }  
  30.           
  31.         @Override  
  32.         public void beforeTextChanged(CharSequence text, int start, int count,int after) {  
  33.         //text  输入框中改变前的字符串信息   
  34.         //start 输入框中改变前的字符串的起始位置   
  35.         //count 输入框中改变前后的字符串改变数量一般为0   
  36.         //after 输入框中改变后的字符串与起始位置的偏移量   
  37.         System.out.println(text.toString());  
  38.         textView0.setText("输入前字符串 [ " + text.toString() + " ]起始光标 [ " + start + " ]结束偏移量  [" + after + " ]");  
  39.         }  
  40.           
  41.         @Override  
  42.         public void afterTextChanged(Editable edit) {  
  43.         //edit  输入结束呈现在输入框中的信息   
  44.         textView2.setText("输入结束后的内容为 [" + edit.toString()+" ] 即将显示在屏幕上");  
  45.         }  
  46.     });  
  47.       
  48.     super.onCreate(savedInstanceState);  
  49.     }  
  50. }  

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/textviewll"  
  4.     android:orientation="vertical" android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <TextView    
  7.       android:id="@+id/monitor_text0"      
  8.       android:layout_width="fill_parent"     
  9.       android:layout_height="wrap_content"   
  10.       android:textSize="18dip"  
  11.       android:textColor="#FF0000"/>   
  12.     <TextView    
  13.       android:id="@+id/monitor_text1"      
  14.       android:layout_width="fill_parent"     
  15.       android:layout_height="wrap_content"  
  16.       android:textSize="18dip"  
  17.       android:textColor="#FF0000"  
  18.       />   
  19.     <TextView    
  20.       android:id="@+id/monitor_text2"      
  21.       android:layout_width="fill_parent"     
  22.       android:layout_height="wrap_content"  
  23.       android:textSize="18dip"  
  24.       android:textColor="#FF0000"  
  25.       />   
  26.     <EditText    
  27.       android:id="@+id/monitor_edit_text0"      
  28.       android:layout_width="fill_parent"     
  29.       android:layout_height="wrap_content"   
  30.       android:hint="监听软键盘按键的输入状态"/>   
  31. </LinearLayout>  

最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2011年资料/Android入门教程/Android软件开发之EditText 详解/

linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款