【转】EditText监听方法,实时的判断输入多少字符

最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能。在EditText提供了一个方法addTextChangedListener实现对输入文本的监控。下边是我自己写的一个Demo。

代码实现:

布局文件main.xml

[html] view plaincopy

  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  android:id="@+id/tv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:textColor="@android:color/white"
  11. android:text="Please input the text:"
  12. />
  13. <EditText android:id="@+id/ET"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. </LinearLayout>

Activity

[java] view plaincopy

    1. package com.damai.test;
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. import android.text.Editable;
    5. import android.text.TextWatcher;
    6. import android.widget.EditText;
    7. import android.widget.TextView;
    8. import android.widget.Toast;
    9. public class TestActivity extends Activity {
    10. private TextView mTextView;
    11. private EditText mEditText;
    12. @Override
    13. public void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.main);
    16. mTextView = (TextView)findViewById(R.id.tv);
    17. mEditText = (EditText)findViewById(R.id.ET);
    18. mEditText.addTextChangedListener(mTextWatcher);
    19. }
    20. TextWatcher mTextWatcher = new TextWatcher() {
    21. private CharSequence temp;
    22. private int editStart ;
    23. private int editEnd ;
    24. @Override
    25. public void onTextChanged(CharSequence s, int start, int before, int count) {
    26. // TODO Auto-generated method stub
    27. temp = s;
    28. }
    29. @Override
    30. public void beforeTextChanged(CharSequence s, int start, int count,
    31. int after) {
    32. // TODO Auto-generated method stub
    33. //          mTextView.setText(s);//将输入的内容实时显示
    34. }
    35. @Override
    36. public void afterTextChanged(Editable s) {
    37. // TODO Auto-generated method stub
    38. editStart = mEditText.getSelectionStart();
    39. editEnd = mEditText.getSelectionEnd();
    40. mTextView.setText("您输入了" + temp.length() + "个字符");
    41. if (temp.length() > 10) {
    42. Toast.makeText(TestActivity.this,
    43. "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
    44. .show();
    45. s.delete(editStart-1, editEnd);
    46. int tempSelection = editStart;
    47. mEditText.setText(s);
    48. mEditText.setSelection(tempSelection);
    49. }
    50. }
    51. };
    52. }
时间: 2024-08-01 15:48:16

【转】EditText监听方法,实时的判断输入多少字符的相关文章

EditText监听方法,实时的判断输入多少字符

最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能.在EditText提供了一个方法addTextChangedListener实现对输入文本的监控.下边是我自己写的一个Demo. 代码实现: 布局文件main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.

Android EditText监听小数点让用户只能输入小数点后两位

最近项目需要输入价格,但是不想让用户在小数点后面输入太多,所以我封装了一个.当用户输入小数点的时候 监听小数点后面的位数,只要大于两位就立马删掉,封装好了,直接可以拿过来用! public static void setPricePoint(final EditText editText) { editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s

android editText 监听事件

在软键盘中注意 在监听的 edittext中 使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:singleline="true" 在activity_main.xml文件中,定义了8个EditText,imeOptions分别是: actionDone 完成 对应 EditorInfo.IME_ACTION_DONE actionGo 前进 对应 EditorInfo.IME_ACTION

Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法

时隔一个多月我又回来啦! 坚持真的是很难的一件事,其它事情稍忙,就很容易说服自己把写博客的计划给推迟了. 好在终于克服了自己的惰性,今天又开始了. 本篇继续我的Luaframework学习之路. 一.规范开发模式 此前的示例中,动态加载的panel都默认以GuiCamera为父节点,且面板的大小设置得有些随意,为方便后续开发,现做一些调整和规范. 1.设定本项目的开发分辨率为1334x750(Game视图分辨率也设置为这个大小): 2.调整相机,将原有的GuiCamera从Canvas下拖离出来

代码添加一个按钮及监听方法

有时候无法从控件中拖拽一个按钮到storyboard,必须用编写代码方式添加按钮: 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 //计算出展示表情区域的宽和 展示区距顶部的高度+10个偏移量 6 //添加按钮 9 //创建button 10 addBtn = [[UIButton alloc

简述Java中Http/Https请求监听方法

一.工欲善其事必先利其器 做Web开发的人总免不了与Http/Https请求打交道,很多时候我们都希望能够直观的的看到我们发送的请求参数和服务器返回的响应信息,这个时候就需要借助于某些工具啦.本文将采用Fiddler2作为分析工具,Fiddler很强大,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据,是越墙抓包之利器.关于工具的介绍可以参考下面的链接: http://www.cnblogs.com/TankXiao/archive/2012/02

OnClickListener两种监听方法

1 //1种:接口OnClickListener ,在onclick响应 2 public class MainActivity extends Activity implements OnClickListener { //这个类前提要接口OnClickListener 3 4 private Button receive; //按钮的初始化 5 private Button falsewifiButton; 6 7 public void init(){ 8 receive=(Button)

Android之如何给ListView添加监听方法

第一步创建ListView //根据id获取对象 ListView listView = (ListView) findViewById(R.id.listview1); //创建适配器对 //参数代表的意思(上下文对象,每行样式类型(必须是TextView类型 android.R.....是系统自带的样式,也可以使用自己定义的),数据本身(可以来自资源数据,也可以来自数组在java中添加)) //以下方式(数据来自资源文件的数组) ListAdapter adapter = ArrayAdap

正则判断输入的字符(英文、数字、空格、其他)的个数

1 /** 2 * 3 * 类 描 述: 正则判断输入的字符(英文.数字.空格.其他)的个数 4 * 作 者: 赵 鹏 5 */ 6 public class RegularJudgeCharacter { 7 8 public static void main(String[] args) { 9 10 String arg = "safd415 ^&*^*^$# "; 11 12 //将string字符串转义为char数组 13 char[] charArray = arg