zoukankan      html  css  js  c++  java
  • 【转】EditText监听方法,实时的判断输入多少字符

    最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能。在EditText提供了一个方法addTextChangedListener实现对输入文本的监控。下边是我自己写的一个Demo。

    代码实现:

    布局文件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.             // 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. }  
  • 相关阅读:
    Qt发送事件与自定义事件
    Qt文本流和数据流
    Qt文件操作
    Qt 布局管理器
    Qt 标准对话框
    Qt 对话框QDailog及其类型
    Qt 对象间的父子关系
    Qt 信号与槽
    Qt 计算器界面实现
    C++ 多态、虚函数(virtual 关键字)、静态联编、动态联编
  • 原文地址:https://www.cnblogs.com/gkwang/p/3934559.html
Copyright © 2011-2022 走看看