700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【转】android 中如何限制 EditText 最大输入字符数

【转】android 中如何限制 EditText 最大输入字符数

时间:2019-12-07 05:35:40

相关推荐

【转】android 中如何限制 EditText 最大输入字符数

原文网址:/fulinwsuafcie/article/details/7437768

方法一:

在 xml 文件中设置文本编辑框属性作字符数限制

如:android:maxLength="10" 即限制最大输入字符个数为10

方法二:

在代码中使用InputFilter 进行过滤

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20

[java]view plaincopy publicclassTextEditActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);EditTexteditText=(EditText)findViewById(R.id.entry);editText.setFilters(newInputFilter[]{newInputFilter.LengthFilter(20)});}}

方法三:

利用 TextWatcher 进行监听

[java]view plaincopy packagecie.textEdit;importandroid.text.Editable;importandroid.text.Selection;importandroid.text.TextWatcher;importandroid.widget.EditText;/**监听输入内容是否超出最大长度,并设置光标位置**/publicclassMaxLengthWatcherimplementsTextWatcher{privateintmaxLen=0;privateEditTexteditText=null;publicMaxLengthWatcher(intmaxLen,EditTexteditText){this.maxLen=maxLen;this.editText=editText;}publicvoidafterTextChanged(Editablearg0){//TODOAuto-generatedmethodstub}publicvoidbeforeTextChanged(CharSequencearg0,intarg1,intarg2,intarg3){//TODOAuto-generatedmethodstub}publicvoidonTextChanged(CharSequencearg0,intarg1,intarg2,intarg3){//TODOAuto-generatedmethodstubEditableeditable=editText.getText();intlen=editable.length();if(len>maxLen){intselEndIndex=Selection.getSelectionEnd(editable);Stringstr=editable.toString();//截取新字符串StringnewStr=str.substring(0,maxLen);editText.setText(newStr);editable=editText.getText();//新字符串的长度intnewLen=editable.length();//旧光标位置超过字符串长度if(selEndIndex>newLen){selEndIndex=editable.length();}//设置新光标所在的位置Selection.setSelection(editable,selEndIndex);}}}

对应的 activity 部分的调用为:

[java]view plaincopy packagecie.textEdit;importandroid.app.Activity;importandroid.os.Bundle;importandroid.text.InputFilter;importandroid.widget.EditText;publicclassTextEditActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);EditTexteditText=(EditText)findViewById(R.id.entry);editText.addTextChangedListener(newMaxLengthWatcher(10,editText));}}

限制输入字符数为10个

main.xml 文件

[html]view plaincopy <?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/label"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Typehere:"/><EditTextandroid:id="@+id/entry"android:singleLine="true"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@android:drawable/editbox_background"android:layout_below="@id/label"/><Buttonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/entry"android:layout_alignParentRight="true"android:layout_marginLeft="10dip"android:text="OK"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@id/ok"android:layout_alignTop="@id/ok"android:text="Cancel"/></RelativeLayout>

效果为输入了10个字符后,光标停在末尾

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。