Android中编辑文本时使用TextWatcher和InputFilter.Length
来源:互联网 发布:linux vi 序号 编辑:程序博客网 时间:2024/05/14 05:20
当在EditText或AutoCompleteTextView中编辑内容时,给它限制最大字符,有时在xml文件中设置maxLength属性是失效的,因此可以用InputFilter实现,而监听编辑文本时,用TextWatcher;
1.InputFilter:InputFilters can be attached toEditable
s to constrain the changes that can be made to them.
InputFilter.Length:This filter will constrain edits not to make the length of the text greater than the specified length.对编辑的内容设置指定的字符数,从而限制其字符长度;
InputFilter.AllCaps:This filter will capitalize all the lower case letters that are added through edits.即将输入的小写字母转换成大写字母;
2.TextWatcher:When an object of a type is attached to an Editable, its methods will be called when the text is changed.
实现TextWatcher重写三个方法:
afterTextChanged(Editable s):This method is called to notify you that, somewhere within s
, the text has been changed.
beforeTextChanged(CharSequence s, int start, int count, int after):This method is called to notify you that, withins
, thecount
characters beginning at start
are about to be replaced by new text with lengthafter
.
onTextChanged(CharSequence s, int start, int before, int count):This method is called to notify you that, withins
, thecount
characters beginning at start
have just replaced old text that had lengthbefore
.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_text" android:inputType="textMultiLine|textCapWords"/></RelativeLayout>在values文件夹中新建integers.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <integer name="edit_max_length">10</integer></resources>java代码:
public class MainActivity extends Activity {private EditText editText;private int maxLength;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText) findViewById(R.id.editText);// 获取需要指定的字符长度maxLength = getResources().getInteger(R.integer.edit_max_length);// 设置TextWatcher监听editText.addTextChangedListener(new MyEditTextMaxLength(maxLength, this));// 使用InputFilter进行过滤,设置最大字符数、英文字母全部大写editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength),new InputFilter.AllCaps() });/** * 注意;若这里再setFilter();则会覆盖上面的。 */// editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});}class MyEditTextMaxLength implements TextWatcher {private int maxLength;private Context context;public MyEditTextMaxLength(int maxLength, Context context) {this.context = context;this.maxLength = maxLength;}/** * 编辑内容未改变之前回调 */@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}/** * 编辑内容改变时回调 */@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}/** * 编辑内容改变之后回调 */@Overridepublic void afterTextChanged(Editable s) {if (s.toString().length() >= maxLength) {Toast.makeText(context, R.string.toast_content,Toast.LENGTH_SHORT).show();}}}}运行效果:达到maxLength之后就不在输入内容,并且Toast提示用户已达到最大字符限制;
- Android中编辑文本时使用TextWatcher和InputFilter.Length
- InputFilter详解、TextWatcher详解
- android 使用InputFilter 对金额和长度进行限制
- Android-- EditText与InputFilter实现文本对比
- Android-- EditText与InputFilter实现文本对比
- Android TextWatcher使用详解
- Android - InputFilter
- androidの监听EditView中的文本TextWatcher
- Android 文本监听接口TextWatcher详解
- Android 文本监听接口TextWatcher详解
- Android 文本监听接口TextWatcher详解
- Android中的TextWatcher的使用
- Android中使用 InputFilter 实现输入的字符限制字母为大/小写
- Android之利用TextWatcher制作自定义编辑文本框
- TextWatcher:监听编辑框EditText中内容的改变
- TextWatcher编辑框监听器
- TextWatcher编辑框监听器
- TextWatcher编辑框监听器
- actionbar
- pycaffe学习
- performSelector最多只能传两个参数
- 不同库 不同表 不同字段 数据同步 触发器
- 代码注释(code comments)
- Android中编辑文本时使用TextWatcher和InputFilter.Length
- svn回退到某个版本
- 判断一个树是否为另一个树的子树
- 如何去年gedit编辑文件时生成的临时文件
- Matlab中,几个取整(fix,round,ceil,floor)的区别
- 串行接口芯片16550
- SVN解决冲突
- androidStudio 中文编译错误: GBK 的不可映射字符。(针对网上通用资料无法解决办法)
- Android自动化测试MonkeyRunner(一)------环境搭建