Method One:
Set the text edit box property as a character limit in an XML file
such as: Android:maxlength= "10" that is, limit the maximum number of input characters is 10
Method Two:
Using Inputfilter in your code to filter
Edittext.setfilters (New Inputfilter[]{new Inputfilter.lengthfilter (20)}); That is, limit the maximum number of input characters to 20
[Java]View Plaincopy
- Public class Texteditactivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- EditText EditText = (EditText) Findviewbyid (r.id.entry);
- Edittext.setfilters (new inputfilter[]{new Inputfilter.lengthfilter (20)});
- }
- }
Method Three:
Use Textwatcher for monitoring
[Java]View Plaincopy
- Package cie.textedit;
- Import android.text.Editable;
- Import android.text.Selection;
- Import Android.text.TextWatcher;
- Import Android.widget.EditText;
- /*
- * Listen for input content exceeding maximum length and set cursor position
- * */
- Public class Maxlengthwatcher implements Textwatcher {
- private int maxlen = 0;
- private EditText EditText = null;
- Public maxlengthwatcher (int maxlen, EditText EditText) {
- This.maxlen = MaxLen;
- this.edittext = EditText;
- }
- public void aftertextchanged (Editable arg0) {
- //TODO auto-generated method stub
- }
- public void beforetextchanged (charsequence arg0, int arg1, int arg2,
- int Arg3) {
- //TODO auto-generated method stub
- }
- public void ontextchanged (charsequence arg0, int arg1, int arg2, int arg3) {
- //TODO auto-generated method stub
- Editable Editable = Edittext.gettext ();
- int len = Editable.length ();
- if (len > MaxLen)
- {
- int selendindex = selection.getselectionend (editable);
- String str = editable.tostring ();
- //Intercept new string
- String newstr = str.substring (0,maxlen);
- Edittext.settext (NEWSTR);
- Editable = Edittext.gettext ();
- //Length of new string
- int newlen = Editable.length ();
- //old cursor position exceeds string length
- if (Selendindex > Newlen)
- {
- Selendindex = Editable.length ();
- }
- //Set the location where the new cursor is located
- Selection.setselection (editable, selendindex);
- }
- }
- }
The call to the corresponding activity part is:
[Java]View Plaincopy
- Package cie.textedit;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.text.InputFilter;
- Import Android.widget.EditText;
- Public class Texteditactivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- EditText EditText = (EditText) Findviewbyid (r.id.entry);
- Edittext.addtextchangedlistener (new Maxlengthwatcher (Ten, EditText));
- }
- }
Limit the number of input characters to 10
Main.xml file
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/label"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Type here:"/>
- <EditText
- android:id="@+id/entry"
- android:singleline="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:d rawable/editbox_background"
- android:layout_below="@id/label"/>
- <Button
- android: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" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toleftof="@id/ok"
- android:layout_aligntop="@id/ok"
- android:text="Cancel" />
- </relativelayout>
The cursor stops at the end after the effect has entered 10 characters
How to limit EditText maximum number of input characters in Android