Android中编辑文本时使用TextWatcher和InputFilter.Length

来源:互联网 发布:linux vi 序号 编辑:程序博客网 时间:2024/05/14 05:20

当在EditText或AutoCompleteTextView中编辑内容时,给它限制最大字符,有时在xml文件中设置maxLength属性是失效的,因此可以用InputFilter实现,而监听编辑文本时,用TextWatcher;

1.InputFilter:InputFilters can be attached toEditables 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.                          

例:EditText中输入内容时,限制字符数为10;
xml布局:
<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提示用户已达到最大字符限制;

0 0
原创粉丝点击