现在的位置: 首页 > 综合 > 正文

TextWatcher监控EditText TextWatcher监控EditText

2013年07月18日 ⁄ 综合 ⁄ 共 3082字 ⁄ 字号 评论关闭
 

TextWatcher监控EditText

分类: Android 586人阅读 评论(0) 收藏 举报

1. main xml

[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: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:ellipsize="marquee"   
  12.     android:focusable="true"   
  13.     android:marqueeRepeatLimit="marquee_forever"   
  14.     android:focusableInTouchMode="true"   
  15.     android:scrollHorizontally="true"      
  16.     android:text="Please input the text:"  
  17.     />  
  18. <EditText android:id="@+id/ET"   
  19.     android:layout_width="match_parent"   
  20.     android:layout_height="wrap_content"  
  21.     android:inputType="number"/>  
  22. </LinearLayout>  

 

java代码:

[java] view
plain
copy

  1. package com.android.text;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.util.Log;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class TextWatcherDemo extends Activity {  
  13.     private TextView mTextView;  
  14.     private EditText mEditText;  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         mTextView = (TextView)findViewById(R.id.tv);  
  21.         mEditText = (EditText)findViewById(R.id.ET);  
  22.         mEditText.addTextChangedListener(mTextWatcher);  
  23.     }  
  24.     TextWatcher mTextWatcher = new TextWatcher() {  
  25.         private CharSequence temp;  
  26.         private int editStart ;  
  27.         private int editEnd ;  
  28.         @Override  
  29.         public void beforeTextChanged(CharSequence s, int arg1, int arg2,  
  30.                 int arg3) {  
  31.             temp = s;  
  32.         }  
  33.          
  34.         @Override  
  35.         public void onTextChanged(CharSequence s, int arg1, int arg2,  
  36.                 int arg3) {  
  37.             mTextView.setText(s);  
  38.         }  
  39.          
  40.         @Override  
  41.         public void afterTextChanged(Editable s) {  
  42.             editStart = mEditText.getSelectionStart();  
  43.             editEnd = mEditText.getSelectionEnd();  
  44.             if (temp.length() > 10) {  
  45.                 Toast.makeText(TextWatcherDemo.this,  
  46.                         "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)  
  47.                         .show();  
  48.                 s.delete(editStart-1, editEnd);  
  49.                 int tempSelection = editStart;  
  50.                 mEditText.setText(s);  
  51.                 mEditText.setSelection(tempSelection);  
  52.             }  
  53.         }  
  54.     };  
  55. }  
  56. 转:http://blog.csdn.net/xufenghappy6/article/details/7345161

1. main xml

[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:orientation="vertical"  
  4.     

抱歉!评论已关闭.