How to limit EditText maximum number of input characters in Android

Source: Internet
Author: User

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
  1. Public class Texteditactivity extends Activity {
  2. /** Called when the activity is first created. * /
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.main);
  7. EditText EditText = (EditText) Findviewbyid (r.id.entry);
  8. Edittext.setfilters (new inputfilter[]{new Inputfilter.lengthfilter (20)});
  9. }
  10. }


Method Three:

Use Textwatcher for monitoring

[Java]View Plaincopy
  1. Package cie.textedit;
  2. Import android.text.Editable;
  3. Import android.text.Selection;
  4. Import Android.text.TextWatcher;
  5. Import Android.widget.EditText;
  6. /*
  7. * Listen for input content exceeding maximum length and set cursor position
  8. * */
  9. Public class Maxlengthwatcher implements Textwatcher {
  10. private int maxlen = 0;
  11. private EditText EditText = null;
  12. Public maxlengthwatcher (int maxlen, EditText EditText) {
  13. This.maxlen = MaxLen;
  14. this.edittext = EditText;
  15. }
  16. public void aftertextchanged (Editable arg0) {
  17. //TODO auto-generated method stub
  18. }
  19. public void beforetextchanged (charsequence arg0, int arg1, int arg2,
  20. int Arg3) {  
  21. //TODO auto-generated method stub
  22. }
  23. public void ontextchanged (charsequence arg0, int arg1, int arg2, int arg3) {
  24. //TODO auto-generated method stub
  25. Editable Editable = Edittext.gettext ();
  26. int len = Editable.length ();
  27. if (len > MaxLen)
  28. {
  29. int selendindex = selection.getselectionend (editable);
  30. String str = editable.tostring ();
  31. //Intercept new string
  32. String newstr = str.substring (0,maxlen);
  33. Edittext.settext (NEWSTR);
  34. Editable = Edittext.gettext ();
  35. //Length of new string
  36. int newlen = Editable.length ();
  37. //old cursor position exceeds string length
  38. if (Selendindex > Newlen)
  39. {
  40. Selendindex = Editable.length ();
  41. }
  42. //Set the location where the new cursor is located
  43. Selection.setselection (editable, selendindex);
  44. }
  45. }
  46. }


The call to the corresponding activity part is:

[Java]View Plaincopy
  1. Package cie.textedit;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.text.InputFilter;
  5. Import Android.widget.EditText;
  6. Public class Texteditactivity extends Activity {
  7. /** Called when the activity is first created. * /
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. super.oncreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. EditText EditText = (EditText) Findviewbyid (r.id.entry);
  13. Edittext.addtextchangedlistener (new Maxlengthwatcher (Ten, EditText));
  14. }
  15. }

Limit the number of input characters to 10

Main.xml file

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:id="@+id/label"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="Type here:"/>
  10. <EditText
  11. android:id="@+id/entry"
  12. android:singleline="true"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:background="@android:d rawable/editbox_background"
  16. android:layout_below="@id/label"/>
  17. <Button
  18. android:id="@+id/ok"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@id/entry"
  22. android:layout_alignparentright="true"
  23. android:layout_marginleft="10dip"
  24. android:text="OK" />
  25. <Button
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_toleftof="@id/ok"
  29. android:layout_aligntop="@id/ok"
  30. android:text="Cancel" />
  31. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.