Android TextWatcher监控EditText中的输入内容并限制其个数

作者:GB_speak 时间:2022-08-18 13:27:41 

布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中输入的内容,作个笔记。

主布局:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView android:id="@+id/tv"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textColor="@android:color/white"
 android:ellipsize="marquee"
 android:focusable="true"
 android:marqueeRepeatLimit="marquee_forever"
 android:focusableInTouchMode="true"
 android:scrollHorizontally="true"  
 android:text="Please input the text:"
 />
<EditText android:id="@+id/ET"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:inputType="number"/>
</LinearLayout>

java代码:


package com.android.text;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextWatcherDemo extends Activity {
 private TextView mTextView;
 private EditText mEditText;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   mTextView = (TextView)findViewById(R.id.tv);
   mEditText = (EditText)findViewById(R.id.ET);
   mEditText.addTextChangedListener(mTextWatcher);
 }
 TextWatcher mTextWatcher = new TextWatcher() {
   private CharSequence temp;
   private int editStart ;
   private int editEnd ;
   @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) {
     mTextView.setText(s);
   }

@Override
   public void afterTextChanged(Editable s) {
     editStart = mEditText.getSelectionStart();
     editEnd = mEditText.getSelectionEnd();
     if (temp.length() > 10) {
       Toast.makeText(TextWatcherDemo.this,
           "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
           .show();
       s.delete(editStart-1, editEnd);
       int tempSelection = editStart;
       mEditText.setText(s);
       mEditText.setSelection(tempSelection);
     }
   }
 };
}

来源:http://www.jianshu.com/p/4c5cba901f66

标签:edittext,textwatcher
0
投稿

猜你喜欢

  • rocketmq如何修改存储路径

    2022-08-20 08:00:57
  • Android存储字符串数据到txt文件

    2021-11-05 21:55:16
  • Spring Boot日志技术logback原理及配置解析

    2022-09-14 10:51:57
  • 全局记录Feign的请求和响应日志方式

    2021-08-19 18:48:02
  • java HttpClient传输json格式的参数实例讲解

    2023-08-08 13:21:26
  • Android自定义双向滑动控件

    2022-04-30 04:11:18
  • 重新认识Java的System.in

    2023-08-24 01:55:18
  • Android Flutter实现3D动画效果示例详解

    2022-04-12 19:34:35
  • Java设计模式之Strategy模式

    2023-11-21 03:58:22
  • Spring Bean的线程安全问题

    2023-06-07 17:15:36
  • 使用chatgpt实现微信聊天小程序的代码示例

    2022-04-26 17:18:24
  • Android基础控件RadioGroup使用方法详解

    2022-08-05 17:43:31
  • java实现的新浪微博分享代码实例

    2023-07-06 08:33:13
  • JAVA读取文件夹大小的几种方法实例

    2021-05-24 21:01:53
  • Springboot项目中使用redis的配置详解

    2021-11-26 03:43:44
  • Java初学者问题图解(动力节点Java学院整理)

    2023-10-15 18:06:11
  • Android 8.0系统中应用图标的适配微技巧

    2022-09-29 00:22:26
  • 详解AOP与Filter拦截请求打印日志实用例子

    2021-09-26 22:03:10
  • 使用注解@Validated效验VO参数是否合规

    2023-10-27 20:13:01
  • c#中使用BackgroundWorker的实现

    2023-05-04 08:51:01
  • asp之家 软件编程 m.aspxhome.com