码迷,mamicode.com
首页 > 其他好文 > 详细

限制EditText 输入的字节数

时间:2016-03-10 12:38:56      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1、代码   

 1         name_tv = (EditText) findViewById( R.id.name_tv );
 2         
 3         name_tv.addTextChangedListener(new TextWatcher() {
 4             @Override
 5             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 6             }
 7 
 8             @Override
 9             public void onTextChanged(CharSequence s, int start, int before, int count) {
10             }
11 
12             @Override
13             public void afterTextChanged(Editable s) {
14                 String string = s.toString().trim();
15 
16                 //限定输入框最多输入18个字节
17                 int inputLength = string.length();
18                 int byteLength = string.getBytes().length;
19                 if (byteLength >= 18) {
20                     name_tv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(inputLength)});
21                 } else {
22                     name_tv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18)});
23                 }
24                 name_tv.setSelection(name_tv.getText().toString().length());
25             }
26         });

 

限制EditText 输入的字节数

标签:

原文地址:http://www.cnblogs.com/zhaoyanjun/p/5261040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!