[Go] edittext listening method, which determines in real time how many characters are input

Source: Internet
Author: User

Recently I was writing a small project, one of which used the function of displaying the number of characters entered in edittext, such as the number of characters remaining in Weibo. The addtextchangedlistener method is provided in edittext to monitor the input text. Below is a demo I wrote myself.

Code implementation:

Layout file main. xml

 

[HTML]View plaincopy
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Textview Android: Id = "@ + ID/TV"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: textcolor = "@ Android: color/White"
  11. Android: text = "Please input the text :"
  12. />
  13. <Edittext Android: Id = "@ + ID/ET"
  14. Android: layout_width = "match_parent"
  15. Android: layout_height = "wrap_content"
  16. />
  17. </Linearlayout>

Activity

[Java]View plaincopy
    1. Package com. Damai. test;
    2. Import Android. App. activity;
    3. Import Android. OS. Bundle;
    4. Import Android. Text. editable;
    5. Import Android. Text. textwatcher;
    6. Import Android. widget. edittext;
    7. Import Android. widget. textview;
    8. Import Android. widget. Toast;
    9. Public class testactivity extends activity {
    10. Private textview mtextview;
    11. Private edittext medittext;
    12. @ Override
    13. Public void oncreate (bundle savedinstancestate ){
    14. Super. oncreate (savedinstancestate );
    15. Setcontentview (R. layout. Main );
    16. Mtextview = (textview) findviewbyid (R. Id. TV );
    17. Medittext = (edittext) findviewbyid (R. Id. Et );
    18. Medittext. addtextchangedlistener (mtextwatcher );
    19. }
    20. Textwatcher mtextwatcher = new textwatcher (){
    21. Private charsequence temp;
    22. Private int editstart;
    23. Private int editend;
    24. @ Override
    25. Public void ontextchanged (charsequence S, int start, int before, int count ){
    26. // Todo auto-generated method stub
    27. Temp = s;
    28. }
    29. @ Override
    30. Public void beforetextchanged (charsequence S, int start, int count,
    31. Int after ){
    32. // Todo auto-generated method stub
    33. // Mtextview. settext (s); // display the input content in real time
    34. }
    35. @ Override
    36. Public void aftertextchanged (editable s ){
    37. // Todo auto-generated method stub
    38. Editstart = medittext. getselectionstart ();
    39. Editend = medittext. getselectionend ();
    40. Mtextview. settext ("You entered" + temp. Length () + "characters ");
    41. If (temp. Length ()> 10 ){
    42. Toast. maketext (testactivity. This,
    43. "The number of words you entered exceeds the limit! ", Toast. length_short)
    44. . Show ();
    45. S. Delete (editStart-1, editend );
    46. Int tempselection = editstart;
    47. Medittext. settext (s );
    48. Medittext. setselection (tempselection );
    49. }
    50. }
    51. };
    52. }

[Go] edittext listening method, which determines in real time how many characters are input

Related Article

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.