Android---3 ways to limit the number of editview input words (reprint)

Source: Internet
Author: User

Method One: Using Textwatcher

Java code
  1. Edittext.addtextchangedlistener (new Textwatcher () {
  2. Private Charsequence temp;
  3. Private Boolean isedit = true;
  4. private int selectionstart;
  5. private int selectionend;
  6. @Override
  7. public void beforetextchanged (charsequence s, int arg1, int arg2,
  8. int Arg3) {  
  9. temp = s;
  10. }
  11. @Override
  12. public void ontextchanged (charsequence s, int arg1, int arg2,
  13. int Arg3) {  
  14. }
  15. @Override
  16. public void aftertextchanged (Editable s) {
  17. SelectionStart = Edittext.getselectionstart ();
  18. Selectionend = Edittext.getselectionend ();
  19. LOG.I ("Gongbiao1", "+selectionstart");
  20. if (temp.length () > Constant.text_max) {
  21. Toast.maketext (kaguhomeactivity. This,
  22. R.string.edit_content_limit, Toast.length_short)
  23. . Show ();
  24. S.delete (selectionstart-1, selectionend);
  25. int tempselection = SelectionStart;
  26. Edittext.settext (s);
  27. Edittext.setselection (tempselection);
  28. }
  29. }
  30. });

Method Two: Using Inputfilter

Java code
    1. Edittext.setfilters (new inputfilter[]{new Inputfilter.lengthfilter (100)}); //100 maximum input words

Method Three: Set in XML

XML code
    1. <EditText
    2. .
    3. .
    4. .
    5. android:maxlength="
    6. />

Android---3 ways to limit the number of editview input words (reprint)

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.