Android textwatcher monitors input content in edittext and limits the number of input characters

Source: Internet
Author: User

In the layout, edittext is often used in the android layout, and the content entered in edittext is often restricted. We can use textwatcher to observe the content entered in the input box and take notes.

Main layout:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "Vertical" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent"> <textview Android: Id = "@ + ID/TV" Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: textcolor = "@ Android: color/White"Android: ellipsize = "Marquee" Android: focusable = "True" Android: marqueerepeatlimit = "Marquee_forever" Android: focusableintouchmode = "True" Android: scrollhorizontally = "True" Android: Text = "Please input the text:"/> <edittext Android: Id = "@ + ID/ET" Android: layout_width = "Match_parent" Android: layout_height = "Wrap_content" Android: inputtype = "Number"/> </linearlayout>

JavaCode:

 Package  Com. Android. text;  Import  Android. App. activity;  Import  Android. OS. Bundle;  Import  Android. Text. editable;  Import  Android. Text. textwatcher;  Import  Android. util. log;  Import  Android. widget. edittext; Import  Android. widget. textview;  Import  Android. widget. Toast;  Public   Class Textwatcherdemo Extends  Activity {  Private  Textview mtextview;  Private  Edittext medittext;  /**  Called when the activity is first created.  */ @ Override  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); setcontentview (R. layout. Main); mtextview = (Textview) findviewbyid (R. Id. TV); medittext = (Edittext) findviewbyid (R. Id. Et); medittext. addtextchangedlistener (mtextwatcher);} textwatcher mtextwatcher = New  Textwatcher (){  Private Charsequence temp;  Private   Int  Editstart;  Private   Int  Editend; @ override  Public   Void Beforetextchanged (charsequence s, Int Arg1, Int  Arg2,  Int  Arg3) {temp =S ;}@ override  Public   Void Ontextchanged (charsequence s, Int Arg1, Int  Arg2,  Int  Arg3) {mtextview. settext (s) ;}@ override  Public   Void  Aftertextchanged (editable s) {editstart = Medittext. getselectionstart (); editend =Medittext. getselectionend ();  If (Temp. Length ()> 10 ) {Toast. maketext (textwatcherdemo.  This  , "The number of words you entered exceeds the limit! " , Toast. length_short). Show (); S. Delete (editstart -1 , Editend );  Int Tempselection = Editstart; medittext. settext (s); medittext. setselection (tempselection );}}};} 

 

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.