EditText监听方法实时的判断输入多少字符

EditText提供了一个方法addTextChangedListener实现对输入文本的监控。下边是Demo。

布局文件main.xml

  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

  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.             // TODO Auto-generated method stub  
  30.              temp = s;  
  31.         }  
  32.           
  33.         @Override  
  34.         public void beforeTextChanged(CharSequence s, int start, int count,  
  35.                 int after) {  
  36.             // TODO Auto-generated method stub  
  37. //          mTextView.setText(s);//将输入的内容实时显示  
  38.         }  
  39.           
  40.         @Override  
  41.         public void afterTextChanged(Editable s) {  
  42.             // TODO Auto-generated method stub  
  43.             editStart = mEditText.getSelectionStart();  
  44.             editEnd = mEditText.getSelectionEnd();  
  45.             mTextView.setText("您输入了" + temp.length() + "个字符");  
  46.             if (temp.length() > 10) {  
  47.                 Toast.makeText(TestActivity.this,  
  48.                         "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)  
  49.                         .show();  
  50.                 s.delete(editStart-1, editEnd);  
  51.                 int tempSelection = editStart;  
  52.                 mEditText.setText(s);  
  53.                 mEditText.setSelection(tempSelection);  
  54.             }  
  55.         }  
  56.     };  
  57. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值