歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:監聽EditText的變化

Android開發教程:監聽EditText的變化

日期:2017/3/1 10:36:29   编辑:Linux編程

之前的有關EditText的文章,只是介紹EditText的一些最基本的用法,這次來深入學習一下EditText。

監聽EditText的變化

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

650) this.width=650;">

當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. public class MainActivity extends Activity {
  11. private EditText text;
  12. String str;
  13. @Override
  14. public void 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. public void afterTextChanged(Editable s) {
  23. // TODO Auto-generated method stub
  24. Log.d("TAG","afterTextChanged--------------->");
  25. }
  26. @Override
  27. public void 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. public void 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. private void 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. public void onClick(DialogInterface dialog, int which) {
  54. // TODO Auto-generated method stub
  55. }
  56. });
  57. dialog = builder.create();
  58. dialog.show();
  59. }
  60. }

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>

效果圖:

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

650) this.width=650;" height=310>

Copyright © Linux教程網 All Rights Reserved