Andriod之EditText监听,实时的判断输入多少字符

来源:互联网 发布:游戏网站源码 编辑:程序博客网 时间:2024/05/18 03:52

EditText提供了一个方法addTextChangedListener实现对输入文本的监听。


代码实现:

布局文件main.xml

[html] view plaincopy
  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  android:id="@+id/tv"  
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:textColor="@android:color/white"   
  11.     android:text="Please input the text:"  
  12.     />  
  13. <EditText android:id="@+id/ET"   
  14.     android:layout_width="match_parent"   
  15.     android:layout_height="wrap_content"  
  16.     />  
  17. </LinearLayout>  
Activity
[java] view plaincopy
  1. package com.damai.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.widget.EditText;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. public class TestActivity extends Activity {  

  12.     private TextView mTextView;  
  13.     private EditText mEditText;  

  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         mTextView = (TextView)findViewById(R.id.tv);  
  19.         mEditText = (EditText)findViewById(R.id.ET);  
  20.         mEditText.addTextChangedListener(mTextWatcher);  
  21.     }  
  22.       
  23.     TextWatcher mTextWatcher = new TextWatcher() {  
  24.         private CharSequence temp;  
  25.         private int editStart ;  
  26.         private int editEnd ;  

  27.         @Override  
  28.         public void onTextChanged(CharSequence s, int start, int before, int count) {  
  29.              temp = s;  
  30.         }  
  31.           
  32.         @Override  
  33.         public void beforeTextChanged(CharSequence s, int start, int count,  
  34.                 int after) { 
  35.     //将输入的内容实时显示   
  36. //          mTextView.setText(s);
  37.         }  
  38.           
  39.         @Override  
  40.         public void afterTextChanged(Editable s) {  
  41.             editStart = mEditText.getSelectionStart();  
  42.             editEnd = mEditText.getSelectionEnd();  
  43.             mTextView.setText("您输入了" + temp.length() + "个字符");  

  44.             if (temp.length() > 10) {  
  45.                 Toast.makeText(TestActivity.this"你输入的字数已经超过了限制!", Toast.LENGTH_SHORT) .show();  
  46.                 s.delete(editStart-1, editEnd);  
  47.                 int tempSelection = editStart;  
  48.                 mEditText.setText(s);  
  49.                 mEditText.setSelection(tempSelection);  
  50.             }  
  51.         }  
  52.     };  
  53. }  


转自:http://blog.csdn.net/walker02/article/details/7234458

0 0