热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

监听EditText的变化

http:liangruijun.blog.51cto.com3061169729505之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的

http://liangruijun.blog.51cto.com/3061169/729505

 

之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下EditText。

监听EditText的变化

使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法:

 当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。

 

 

MainActivity.java

package com.lingdududu.watcher; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { private EditText text; String str; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (EditText)findViewById(R.id.text); text.addTextChangedListener(textWatcher); } private TextWatcher textWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub Log.d("TAG","afterTextChanged--------------->"); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub Log.d("TAG","beforeTextChanged--------------->"); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TAG","onTextChanged--------------->"); str = text.getText().toString(); try { //if ((heighText.getText().toString())!=null) Integer.parseInt(str); } catch (Exception e) { // TODO: handle exception showDialog(); } } }; private void showDialog(){ AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error); builder.setMessage("你输出的整型数字有误,请改正"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog = builder.create(); dialog.show(); } }

 


 main.xml


 

 

 效果图:

当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正

在LogCat查看调用这些方法的顺序:

beforeTextChanged-->onTextChanged-->onTextChanged

 

第二个例子实现了提示文本框还能输入多少个字符的功能

 

package com.lingdududu.test; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private Button clearBtn; private EditText et; private TextView tv; final int MAX_LENGTH = 20; int Rest_Length = MAX_LENGTH; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv =(TextView)findViewById(R.id.tv); et = (EditText)findViewById(R.id.et); clearBtn = (Button)findViewById(R.id.btn); et.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { tv.setText("还能输入"+Rest_Length+"个字"); } @Override public void afterTextChanged(Editable s) { tv.setText("还能输入"+Rest_Length+"个字"); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(Rest_Length>0){ Rest_Length = MAX_LENGTH - et.getText().length(); } } }); clearBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { et.setText(""); Rest_Length = MAX_LENGTH; } }); } }

 

 

 

效果图:

 

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/729505


转:https://www.cnblogs.com/misybing/p/5018673.html



推荐阅读
author-avatar
郑予家21342
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有