Android开发教程:监听EditText的变化

本文介绍如何在Android应用中监听EditText控件的文字变化,并通过实例演示了如何限制EditText输入内容的长度及验证输入是否为整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


监听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> 
  效果图:
 
当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-01/52648.htm

 

新历史版本 :Android---3种方式限制EditView输入字数


editText.addTextChangedListener(new TextWatcher() {  
           private CharSequence temp;  
           private boolean isEdit = true;  
           private int selectionStart ;  
           private int selectionEnd ;  
           @Override 
           public void beforeTextChanged(CharSequence s, int arg1, int arg2,  
                   int arg3) {  
               temp = s;  
           }  
             
           @Override 
           public void onTextChanged(CharSequence s, int arg1, int arg2,  
                   int arg3) {  
           }  
             
           @Override 
           public void afterTextChanged(Editable s) {  
                selectionStart = editText.getSelectionStart();  
               selectionEnd = editText.getSelectionEnd();  
               Log.i("gongbiao1",""+selectionStart);  
               if (temp.length() > Constant.TEXT_MAX) {  
                   Toast.makeText(KaguHomeActivity.this,  
                           R.string.edit_content_limit, Toast.LENGTH_SHORT)  
                           .show();  
                   s.delete(selectionStart-1, selectionEnd);  
                   int tempSelection = selectionStart;  
                   editText.setText(s);  
                   editText.setSelection(tempSelection);  
               }  
           }  
 
 
       }); 

editText.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private boolean isEdit = true;
            private int selectionStart ;
            private int selectionEnd ;
            @Override
            public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                    int arg3) {
                temp = s;
            }
           
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                    int arg3) {
            }
           
            @Override
            public void afterTextChanged(Editable s) {
                 selectionStart = editText.getSelectionStart();
                selectionEnd = editText.getSelectionEnd();
                Log.i("gongbiao1",""+selectionStart);
                if (temp.length() > Constant.TEXT_MAX) {
                    Toast.makeText(KaguHomeActivity.this,
                            R.string.edit_content_limit, Toast.LENGTH_SHORT)
                            .show();
                    s.delete(selectionStart-1, selectionEnd);
                    int tempSelection = selectionStart;
                    editText.setText(s);
                    editText.setSelection(tempSelection);
                }
            }


        });

方法二:利用InputFilter

    


editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});  //其中100最大输入字数 

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});  //其中100最大输入字数

方法三:在XML中设定

Xml代码
<EditText 
    .  
    .  
    .  
    android:maxLength="100" 
/> 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值