Android TextWatcher监控EditText中的输入内容并限制其个数
作者:GB_speak 发布时间:2022-08-18 13:27:41
标签:edittext,textwatcher
布局中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


猜你喜欢
- Android中处理图像是一件很常见的事情,这里记录备忘一些亲身使用过的处理图片数据的方法。转为BitmapRGB值转Bitmappriva
- 前言在我们的项目中,通常会把数据存储到关系型数据库中,比如Oracle,SQL Server,Mysql等,但是关系型数据库对于并发的支持并
- 1. 启用AOPa. 在类上添加@Aspect注解b. 注入该类, 可以使用@Component进行注入到Spring容器中2. 通过Poi
- 一、Quartz的特点* 按作业类的继承方式来分,主要有以下两种:1.作业类继承org.springframework.scheduling
- 安卓中为activity创建菜单,供大家参考,具体内容如下1.在res上面右键 > new > Android xml file
- 一、简述1、AOP的概念如果你用java做过后台开发,那么你一定知道AOP这个概念。如果不知道也无妨,套用百度百科的介绍,也能让你明白这玩意
- 在实际的项目开发中会有很多的对象,如何高效、方便地管理对象,成为影响程序性能与可维护性的重要环节。Java 提供了集合框架来解决此类问题,线
- 前言有时候我们会在属性注入的时候添加@Lazy注解实现延迟注入,今天咱们通过阅读源码来分析下原因一、一个简单的小例子代码如下:@Servic
- Sentinel数据双向同步上面实现了Nacos单向同步配置规则到Sentinel,但是只是单向的,没有实现Sentinel向Nacos同步
- 前言:如何在C++代码中调用写好的C接口?你可能会奇怪,C++不是兼容C吗?直接调用不就可以了,那么我们来测试一下,先看看C++如何调用C代
- 某少年宫引进了一批机器人小车。可以接受预先输入的指令,按指令行动。小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干
- 一,定义变量C# 表达式树中,定义一个变量,使用 ParameterExpression。创建变量结点的方法有两种,Express
- String对象是不可变的:意思就是无论是对String的新增或修改,出现一个全新的String内容时,都意味着诞生了一个新的对象。但是如果
- centos下搭建GitLab+Jenkins持续集成环境,供大家参考,具体内容如下1、安装JDKyum install -y java2、
- Java 8支持动态语言,看到了很酷的Lambda表达式,对一直以静态类型语言自居的Java,让人看到了Java虚拟机可以支持动态语言的目标
- SpringBoot中的过滤器 * 操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两
- 前言今天起床,拿起手机开机第一时间当然是打开微信了,左右滑动Viewpager,发现它使用了一种叫惰性加载,或者说懒加载(lazy-load
- 1、添加android support包因为上面的几个类都是在android support包中才提供,我们先添加包。在Eclipse-&g
- 固定的策略有时候还是无法满足千变万化的需求变动,一方面需要支持特定的用户需求,另一方面又得尽可能的复用代码,避免重复开发,这就需要将这部分的
- 被覆盖比较好理解,类似于多态的实现,访问时通过类方法表来访问,你实际是什么类型,访问的方法就是那个类型的方法而不会是你的父类的方法。被隐藏是