textwatcherEditText怎么实现字母过滤功能?
textwatcher 时间:2021-07-20 阅读:(
)
EditText监听方法,实时的判断输入多少字符
最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能。
在EditText提供了一个方法addTextChangedListener实现对输入文本的监控。
下边是我自己写的一个Demo。
代码实现:
布局文件main.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+"
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 plaincopy
.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();
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);
}
}
};
}安卓开发 如何为edittext做一个固定的输入格式
TextWatcher tw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
在这里面做字符串s的正则表达式处理
}
@Override
public void afterTextChanged(Editable s) {
}
};
.addTextChangedListener(tw);android编成:TextChangedListener用法?
TextChangedListener是android文本框改变的一个监听器,一般用于统计文本输入、监测输入是否符合规范等。
要想监听文本框的文本改变情况需要实现:beforeTextChanged、onTextChanged、 afterTextChanged三个方法。
EditText editText = new EditText(this);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
如果要实现监听输入是否符合规范,或统计输入的字数,可以在afterTextChanged方法中书写相应的业务逻辑代码text与ntext的区别
text和ntext的区别?- -
今天在asp中执行sql语句是发生了一点问题,就是text编码问题,大概text类型对编码的支持有点问题,改成ntext就可以了............
也就是说,我们不是要注意在数据库表中"Text" 或"ntext" 类型的字段排在最后,而是要注意"SELECT" 子句中"Text" 或"ntext" 类型的字段排在最后。
如果有一列以上的"Text" 或"ntext" 类型的字段,要注意它们在数据库表中的顺序。
ntext
可变长度 Unicode 数据的最大长度为 230 - 1 (1,073,741,823) 个字符。
存储大小是所输入字符个数的两倍(以字节为单位)。
ntext 在 SQL-92 中的同义词是 national text。
text
服务器代码页中的可变长度非 Unicode 数据的最大长度为 231-1 (2,147,483,647) 个字符。
当服务器代码页使用双字节字符时,存储量仍是 2,147,483,647 字节。
存储大小可能小于 2,147,483,647 字节(取决于字符串)。
text indent是什么意思
text-indent 属性规定文本块中首行文本的缩进。
注释:允许使用负值。
如果使用负值,那么首行会被缩进到左边。
例如: p {text-indent: 1cm}EditText怎么实现字母过滤功能?
EditText is derived from TextView which has avoid addTextChangedListener(TextWatcher watcher)method. TextWatcher has callbacks, likeabstract void afterTextChanged(Editable s)
DogYun怎么样?DogYun是一家2019年成立的国人主机商,称为狗云,提供VPS及独立服务器租用,其中VPS分为经典云和动态云(支持小时计费及随时可删除),DogYun云服务器基于Kernel-based Virtual Machine(Kvm)硬件的完全虚拟化架构,您可以在弹性云中,随时调整CPU,内存,硬盘,网络,IPv4路线(如果该数据中心接入了多条路线)等。DogYun弹性云服务器优...
我们在选择虚拟主机和云服务器的时候,是不是经常有看到有的线路是BGP线路,比如前几天有看到服务商有国际BGP线路和国内BGP线路。这个BGP线路和其他服务线路有什么不同呢?所谓的BGP线路机房,就是在不同的运营商之间通过技术手段时间各个网络的兼容速度最佳,但是IP地址还是一个。正常情况下,我们看到的某个服务商提供的IP地址,在电信和联通移动速度是不同的,有的电信速度不错,有的是移动速度好。但是如果...
atcloud主要提供常规cloud(VPS)和storage(大硬盘存储)系列VPS,其数据中心分布在美国(俄勒冈、弗吉尼亚)、加拿大、英国、法国、德国、新加坡,所有VPS默认提供480Gbps的超高DDoS防御+不限流量,杜绝DDoS攻击骚扰,比较适合海外建站等相关业务。ATCLOUD.NET是一家成立于2020年的海外主机商,主要提供KVM架构的VPS产品、LXC容器化产品、权威DNS智能解...
textwatcher为你推荐
office软件包我下载了一个office软件包,然后在百度文库里下载东西,可是打开后全是乱码,怎么回事啊安卓系统软件删除安卓手机怎么卸载已经安装的各类软件?安卓系统软件删除安卓系统中如何删除无用程序?智能机刷机软件安卓手机在电脑上强制刷机用什么软件好?催收软件哪个好靠谱的催收方式除了正规要账公司,还有哪些渠道的?1518qq几开头的QQ号好handoff怎么用Mac的Hand Off怎么连接iPhone?nero教程NERO怎么使用?罗振宇2017跨年演讲“时间的朋友”跨年演讲办了多少场,分别是什么主题?音响解码音响功放:源码输出和解码输出有什么区别
in域名注册 远程登陆工具 dropbox网盘 谁的qq空间最好看 爱奇艺vip免费领取 搜索引擎提交入口 web服务器是什么 美国凤凰城 防cc攻击 带宽测试 酷锐 asp简介 服务器是什么 9929 瓦工工资 冰盾ddos防火墙 网易轻博客 qq登陆空间 qq空间申请关闭 web服务器配置 更多