標籤:
之前部落格上的有關EditText的文章,只是介紹EditText的一些最基本的用法,這次來深入學習一下EditText。
監聽EditText的變化
使用EditText的addTextChangedListener(TextWatcher watcher)方法對EditText實現監聽,TextWatcher是一個介面類,所以必須實現TextWatcher裡的抽象方法:
當EditText裡面的內容有變化的時候,觸發TextChangedListener事件,就會調用TextWatcher裡面的抽象方法。
-----------------------------------------------------------------------------------------------
MainActivity.java
-----------------------------------------------------------------------------------------------
1 package com.lingdududu.watcher; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.text.Editable; 8 import android.text.TextWatcher; 9 import android.util.Log; 10 import android.widget.EditText; 11 12 public class MainActivity extends Activity { 13 private EditText text; 14 String str; 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 20 text = (EditText)findViewById(R.id.text); 21 text.addTextChangedListener(textWatcher); 22 } 23 24 private TextWatcher textWatcher = new TextWatcher() { 25 26 @Override 27 public void afterTextChanged(Editable s) { 28 // TODO Auto-generated method stub 29 Log.d("TAG","afterTextChanged--------------->"); 30 } 31 32 @Override 33 public void beforeTextChanged(CharSequence s, int start, int count, 34 int after) { 35 // TODO Auto-generated method stub 36 Log.d("TAG","beforeTextChanged--------------->"); 37 } 38 39 @Override 40 public void onTextChanged(CharSequence s, int start, int before, 41 int count) { 42 Log.d("TAG","onTextChanged--------------->"); 43 str = text.getText().toString(); 44 try { 45 //if ((heighText.getText().toString())!=null) 46 Integer.parseInt(str); 47 48 } catch (Exception e) { 49 // TODO: handle exception 50 showDialog(); 51 } 52 53 } 54 }; 55 56 private void showDialog(){ 57 AlertDialog dialog; 58 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 59 builder.setTitle("訊息").setIcon(android.R.drawable.stat_notify_error); 60 builder.setMessage("你輸出的整型數字有誤,請改正"); 61 builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){ 62 @Override 63 public void onClick(DialogInterface dialog, int which) { 64 // TODO Auto-generated method stub 65 66 } 67 }); 68 dialog = builder.create(); 69 dialog.show(); 70 } 71 }
-----------------------------------------------------------------------------------------------------
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 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 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.text.Editable; 6 import android.text.TextWatcher; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.TextView; 11 12 public class MainActivity extends Activity { 13 private Button clearBtn; 14 private EditText et; 15 private TextView tv; 16 final int MAX_LENGTH = 20; 17 int Rest_Length = MAX_LENGTH; 18 @Override 19 public void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 tv =(TextView)findViewById(R.id.tv); 23 et = (EditText)findViewById(R.id.et); 24 25 clearBtn = (Button)findViewById(R.id.btn); 26 27 et.addTextChangedListener(new TextWatcher() { 28 29 @Override 30 public void beforeTextChanged(CharSequence s, int start, int count, 31 int after) { 32 tv.setText("還能輸入"+Rest_Length+"個字"); 33 } 34 35 @Override 36 public void afterTextChanged(Editable s) { 37 tv.setText("還能輸入"+Rest_Length+"個字"); 38 } 39 40 @Override 41 public void onTextChanged(CharSequence s, int start, int before, int count) { 42 if(Rest_Length>0){ 43 Rest_Length = MAX_LENGTH - et.getText().length(); 44 } 45 } 46 }); 47 48 clearBtn.setOnClickListener(new Button.OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 et.setText(""); 52 Rest_Length = MAX_LENGTH; 53 } 54 }); 55 } 56 }
本文出自 “IT的點點滴滴” 部落格,請務必保留此出處http://liangruijun.blog.51cto.com/3061169/729505
【Android進階學習】監聽EditText的變化