EditText监听方法,实时的判断输入多少字符
作者:木头人__walker 时间:2021-08-16 11:32:36
最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能。在EditText提供了一个方法addTextChangedListener实现对输入文本的监控。下边是我自己写的一个Demo。
代码实现:
布局文件main.xml
[html] view plain copy
<?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:text="Please input the text:"
/>
<EditText android:id="@+id/ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Activity
[java] view plain copy
package com.damai.test;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity {
private TextView mTextView;
private EditText mEditText;
@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 onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
temp = s;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
// mTextView.setText(s);//将输入的内容实时显示
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
mTextView.setText("您输入了" + temp.length() + "个字符");
if (temp.length() > 10) {
Toast.makeText(TestActivity.this,
"你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
.show();
s.delete(editStart-1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};
}
来源:http://blog.csdn.net/walker02/article/details/7234458
标签:EditText,监听
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot bootstrap.yml nacos配置中心问题
2022-07-07 08:25:01
![](https://img.aspxhome.com/file/2023/4/77364_0s.png)
Java毕业设计实战项目之宠物商城系统的实现流程
2022-03-02 06:10:41
![](https://img.aspxhome.com/file/2023/7/94237_0s.jpg)
SpringBoot 如何从容器中获取对象
2023-12-06 08:20:40
![](https://img.aspxhome.com/file/2023/5/75775_0s.png)
浅谈Java之终止继承:Final类和Fianl方法
2023-02-14 00:07:08
![](https://img.aspxhome.com/file/2023/7/85537_0s.png)
Android OpenGL仿自如APP裸眼3D效果详解
2023-12-23 21:37:05
![](https://img.aspxhome.com/file/2023/0/88110_0s.png)
Java中ReentrantLock4种常见的坑
2021-09-26 10:51:46
![](https://img.aspxhome.com/file/2023/3/61133_0s.webp)
idea创建JAVA Class时自动生成头部文档注释的方法
2023-07-10 18:53:07
![](https://img.aspxhome.com/file/2023/5/69715_0s.png)
Java如何优雅替换if-else语句
2023-02-23 10:30:27
![](https://img.aspxhome.com/file/2023/8/71868_0s.jpg)
C#、vb.net及SQL判断指定年份是否为闰年的方法
2023-05-18 09:49:55
C# TcpClient网络编程传输文件的示例
2021-10-16 16:01:13
![](https://img.aspxhome.com/file/2023/2/81062_0s.jpg)
Java编写日历表的3种方式
2023-02-16 00:50:31
![](https://img.aspxhome.com/file/2023/8/70978_0s.jpg)
使用Logback日志保存到相对路径的操作
2021-10-11 16:42:30
JAVA如何定义构造函数过程解析
2023-11-04 08:15:09
![](https://img.aspxhome.com/file/2023/7/58477_0s.png)
arthas jprofiler做复杂链路的调用分析
2022-01-15 12:01:25
![](https://img.aspxhome.com/file/2023/6/78956_0s.png)
Java String类常用方法梳理总结
2021-08-22 17:57:03
Java:"失效"的private修饰符
2021-06-28 19:56:48
Android集成Flutter
2023-07-06 13:07:33
![](https://img.aspxhome.com/file/2023/0/101830_0s.jpg)
SpringMVC实现上传下载文件
2022-11-10 03:18:14
![](https://img.aspxhome.com/file/2023/9/72409_0s.jpg)
vue+springboot前后端分离工程跨域问题解决方案解析
2023-08-06 06:51:10
springboot项目如何防止XSS攻击
2021-10-17 10:03:02
![](https://img.aspxhome.com/file/2023/5/64795_0s.png)