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)
农历春节将至,腾讯云开启了热门爆款云产品首单特惠秒杀活动,上海/北京/广州1核2G云服务器首年仅38元起,上架了新的首单优惠活动,每天三场秒杀,长期有效,其中轻量应用服务器2G内存5M带宽仅需年费38元起,其他产品比如CDN流量包、短信包、MySQL、直播流量包、标准存储等等产品也参与活动,腾讯云官网已注册且完成实名认证的国内站用户均可参与。活动页面:https://cloud.tencent.c...
Vultr 商家有新增韩国首尔机房,这个是继日本、新加坡之后的第三个亚洲机房。不过可以大概率知道肯定不是直连中国机房的,因为早期的日本机房有过直连后来取消的。今天准备体验看看VULTR VPS主机商的韩国首尔机房的云服务器的速度和性能。1、全球节点PING速度测试这里先通过PING测试工具看看全球几十个节点的PING速度。看到好像移动速度还不错。2、路由去程测试测试看看VULTR韩国首尔机房的节点...
整理一下CloudCone商家之前推送的闪购VPS云服务器产品,数量有限,活动推出可能很快机器就售罄了,有需要美国便宜VPS云服务器的朋友可以关注一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2017年的美国服务器提供商,国外实力大厂,自己开发的主机系统面板,CloudCone主要销售美国洛杉矶云服务器产品,优势特点是...
textwatcher为你推荐
office软件包不属于MIcrosoft Office软件包的软件是 ? A Mailqq空间维护qq空间相关功能升级维护怎么解除?色中色luntanwww.fzluntan.tk是什么类型的网站啊?iphone12或支持北斗导航苹果12几个版本magento模板magento1.9主题开发用哪个模板为基础好?呼叫中心搭建如何建立适合自己的呼叫中心微软永久关闭实体店微软windows10易升怎么关闭发送验证码怎样向好友发微信验证码oa系统包含哪些模块求问OA办公系统的组成模块能分为哪三个类别?微服务网关手机上网的网关端口和网关IP是多少?
域名停靠 域名中介 in域名注册 日本私人vps 独享100m 博客主机 好玩的桌面 京东云擎 湖南服务器托管 hostloc web服务器搭建 smtp虚拟服务器 网通服务器 江苏双线 贵州电信 免费获得q币 聚惠网 卡巴下载 paypal兑换 饭桶 更多