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

布局中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); } } }; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-07-29 14:41:31

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

Android编程开发之EditText中不输入特定字符会显示相关提示信息的方法

本文实例讲述了Android编程开发之EditText中不输入特定字符会显示相关提示信息的方法.分享给大家供大家参考,具体如下: 先看效果图: 源码如下: 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="

edittext-如何设置用户在 EditText 中的输入行数

问题描述 如何设置用户在 EditText 中的输入行数 用户通过点击输入/下一行的键可以输入超过5行,如何限制用户的输入来固定EditText中的行数? <EditText android:id=""@+id/editText2"" android:layout_height=""wrap_content"" android:layout_width=""fill_parent""

【Android】设置EditText为仅输入数字且最多只能有两位数字

需求很简单,就是要设置一个EditText仅能输入数字且输入的数字中小数部分最多可以有两位. 第一步,很简单,在XML文件中,将EditText的inputType设置成NumberDecimal,多余的属性我就不写出来,只写出主要的部分: <EditText ... android:inputType="numberDecimal" ... /> 第二部,代码中修改EditText 的addTextChangedListener 方法,同样的先上代码,再来解释: Edit

Android EditText实现分割输入内容

在项目中可能会有许多需要输入手机号码.银行卡号或者身份证号等内容的输入框.如果直接输入的话将会是一堆号码堆在一起,第一是不太美观,第二也容易出错,用户体验不太好.但是若将输入的号码按特定格式进行分割将会大大提高用户体验! 以下是对常用的号码进行简单封装的自定义输入框控件,方便我们在开发过程中使用: 该控件支持xml属性指定,也支持代码指定: 该控件支持类型分别为电话号码(000 0000 0000).银行卡号(0000 0000 0000 0000 000)和身份证号(000000 0000 0

android关于实现EditText中加多行下划线的的一种方法

1. 重写EditText public class LinedEditText extends EditText { private Paint linePaint; private float margin; private int paperColor; public LinedEditText(Context paramContext, AttributeSet paramAttributeSet) { super(paramContext, paramAttributeSet); th

android TextView和EditText中显示图片

EditText(TextView同样处理). 添加图片主要用SpannableString和ImageSpan类: Drawable drawable = getResources().getDrawable(R.drawable.icon); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); SpannableString spannable = new Spannab

JTextField中的输入内容为XX-XX-XXXX(X都为数字),输入不符合就报错

问题描述 输入的格式为XX-XX-XXXX(X都为数字),中间的"-"要怎么什么方式加进去 解决方案 解决方案二:MaskFormatterformatter=newMaskFormatter("##-##-解决方案三:#");JTextFieldtf=newJFormattedTextField(formatter);这是一种方式,利用掩码的方式还有一种方式就是在用户输入事件中,使用正则表达式进行判断,如果不通过则弹出提示信息.

android-在Edittext中设置输入电话号码时隐藏字符串

问题描述 在Edittext中设置输入电话号码时隐藏字符串 怎么设置Edittext属性,当输入电话号码时能隐藏字符串,我使用android:inputType=""textPassword""来隐藏字符串,当使用android:inputType=""phone""会出现按钮式拨号簿接口.如何让二者结合起来? 解决方案 android:password is deprecated but AFAIK is the only

Android开发中给EditText控件添加TextWatcher监听实现对输入字数的限制(推荐)_Android

 做这个功能是因为开发项目的时候,由于后台接口的一些参数的值的长度有要求,不能超过多少个字符,所以在编辑框中输入的字符是要有限制的. 下面就来看一下demo的实现过程: 首先,在xml控件中放置一个EditText控件,然后初始化该控件并对该控件添加文本监听.xml自己简单的设计一下,代码较为简单,直接上代码: package com.example.edittext; import android.app.Activity; import android.os.Bundle; import a