1. 程式人生 > >【Android進階學習】監聽EditText的變化

【Android進階學習】監聽EditText的變化

監聽EditText的變化

使用EditText的addTextChangedListener(TextWatcher watcher)方法對EditText實現監聽,TextWatcher是一個介面類,所以必須實現TextWatcher裡的抽象方法:

 當EditText裡面的內容有變化的時候,觸發TextChangedListener事件,就會呼叫TextWatcher裡面的抽象方法。

MainActivity.java

  1. package com.lingdududu.watcher;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6. import android.text.Editable;  
  7. import android.text.TextWatcher;  
  8. import android.util.Log;  
  9. import android.widget.EditText;  
  10. publicclass MainActivity extends Activity {  
  11. private EditText text;  
  12.     String str;  
  13. @Override
  14. publicvoid onCreate(Bundle savedInstanceState) {  
  15. super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         text = (EditText)findViewById(R.id.text);  
  18.         text.addTextChangedListener(textWatcher);  
  19.     }  
  20. private TextWatcher textWatcher = new TextWatcher() {  
  21. @Override
  22. publicvoid afterTextChanged(Editable s) {     
  23. // TODO Auto-generated method stub   
  24.             Log.d("TAG","afterTextChanged--------------->");   
  25.         }   
  26. @Override
  27. publicvoid beforeTextChanged(CharSequence s, int start, int count,  
  28. int after) {  
  29. // TODO Auto-generated method stub
  30.             Log.d("TAG","beforeTextChanged--------------->");  
  31.         }  
  32. @Override
  33. publicvoid onTextChanged(CharSequence s, int start, int before,     
  34. int count) {     
  35.             Log.d("TAG","onTextChanged--------------->");    
  36.             str = text.getText().toString();  
  37. try {  
  38. //if ((heighText.getText().toString())!=null) 
  39.                 Integer.parseInt(str);  
  40.             } catch (Exception e) {  
  41. // TODO: handle exception
  42.                 showDialog();  
  43.             }  
  44.         }                    
  45.     };  
  46. privatevoid showDialog(){  
  47.         AlertDialog dialog;  
  48.         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
  49.         builder.setTitle("訊息").setIcon(android.R.drawable.stat_notify_error);  
  50.         builder.setMessage("你輸出的整型數字有誤,請改正");  
  51.         builder.setPositiveButton("確定"new DialogInterface.OnClickListener(){  
  52. @Override
  53. publicvoid onClick(DialogInterface dialog, int which) {  
  54. // TODO Auto-generated method stub
  55.             }                     
  56.         });  
  57.         dialog = builder.create();  
  58.         dialog.show();  
  59.     }  

 main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="請輸入整型數字"
  11. />
  12. <EditText
  13. android:id="@+id/text"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. </LinearLayout>

  效果圖:

當我們在輸入框輸入不是整型數字的時候,會立刻彈出輸入框,提示你改正

在LogCat檢視呼叫這些方法的順序:

beforeTextChanged-->onTextChanged-->onTextChanged

第二個例子實現了提示文字框還能輸入多少個字元的功能

  1. package com.lingdududu.test;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.text.Editable;  
  5. import android.text.TextWatcher;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. publicclass MainActivity extends Activity {  
  11. private Button clearBtn;  
  12. private EditText et;  
  13. private TextView tv;  
  14. finalint MAX_LENGTH = 20;  
  15. int Rest_Length = MAX_LENGTH;  
  16. @Override
  17. publicvoid onCreate(Bundle savedInstanceState) {  
  18. super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         tv =(TextView)findViewById(R.id.tv);  
  21.         et = (EditText)findViewById(R.id.et);  
  22.         clearBtn = (Button)findViewById(R.id.btn);  
  23.         et.addTextChangedListener(new TextWatcher() {  
  24. @Override
  25. publicvoid beforeTextChanged(CharSequence s, int start, int count,  
  26. int after) {  
  27.                 tv.setText("還能輸入"+Rest_Length+"個字");              
  28.             }  
  29. @Override
  30. publicvoid afterTextChanged(Editable s) {  
  31.                 tv.setText("還能輸入"+Rest_Length+"個字");  
  32.             }  
  33. @Override
  34. publicvoid onTextChanged(CharSequence s, int start, int before, int count) {  
  35. if(Rest_Length>0){  
  36.                     Rest_Length = MAX_LENGTH - et.getText().length();  
  37.                 }  
  38.             }             
  39.         });  
  40.         clearBtn.setOnClickListener(new Button.OnClickListener() {        
  41. @Override
  42. publicvoid onClick(View v) {  
  43.                 et.setText("");  
  44.                 Rest_Length = MAX_LENGTH;  
  45.             }  
  46.         });  
  47.     }  
  48.  } 

效果圖: