"Android Advanced Learning" monitor edittext changes

Source: Internet
Author: User
Tags gettext

Before the blog on the article about EditText, just introduce some of EditText's most basic usage, this time to learn more about EditText.

Monitor the change of EditText

EditText is monitored using EditText's addtextchangedlistener (Textwatcher Watcher) method, Textwatcher is an interface class, So the abstract method in Textwatcher must be implemented:

When the content inside the edittext changes, the Textchangedlistener event is triggered and the abstract method inside the Textwatcher is invoked.

-----------------------------------------------------------------------------------------------

Mainactivity.java

-----------------------------------------------------------------------------------------------

1 Package Com.lingdududu.watcher; 2      3 import android.app.Activity; 4 import Android.app.AlertDialog; 5 import Android.content.DialogInterface; 6 import Android.os.Bundle; 7 import android.text.Editable; 8 import Android.text.TextWatcher; 9 import Android.util.Log; Ten import Android.widget.EditText;  One       A Public class Mainactivity extends Activity { - private EditText text;  - String str;  the @Override -PublicvoidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate);  - Setcontentview (R.layout.main);  +                -Text =(EditText) Findviewbyid (R.id.text);  + Text.addtextchangedlistener (Textwatcher);  A         }   at            -Private Textwatcher Textwatcher =NewTextwatcher () { -                - @Override -Publicvoidaftertextchanged (Editable s) { -                 //TODO auto-generated Method Stub inLOG.D ("TAG", "Aftertextchanged--------------->");  -             }    to                + @Override -PublicvoidBeforetextchanged (Charsequence S,intStartintCount, the                     intAfter ) {   *                 //TODO auto-generated Method Stub $LOG.D ("TAG", "Beforetextchanged--------------->"); Panax Notoginseng             }   -       the @Override +PublicvoidOnTextChanged (Charsequence S,intStartintbefore, A                     intcount) {      theLOG.D ("TAG", "OnTextChanged--------------->");  +str =Text.gettext (). toString ();  -                 Try {   $                     //if ((Heightext.gettext (). toString ())!=null) $ integer.parseint (str);  -                        -}Catch(Exception e) { the                     //Todo:handle Exception - ShowDialog (); Wuyi                 }   the                                    -             }                     Wu         };  -       AboutPrivatevoidShowDialog () { $ Alertdialog Dialog;  -Alertdialog.builder Builder =NewAlertdialog.builder (mainactivity. This);  -Builder.settitle ("Message"). SetIcon (Android.  R.DRAWABLE.STAT_NOTIFY_ERROR);  -Builder.setmessage ("The integer number you output is wrong, please correct");  ABuilder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { + @Override thePublicvoidOnClick (Dialoginterface Dialog,intwhich) {   -                     //TODO auto-generated Method Stub $                        the                 }                      the             });  theDialog =builder.create ();  the dialog.show ();  -         }   in}

-----------------------------------------------------------------------------------------------------

Main.xml

-----------------------------------------------------------------------------------------------------

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<TextView8Android:layout_width= "Fill_parent"9android:layout_height= "Wrap_content"Tenandroid:text= "Please enter integer number" One/> A<EditText -Android:id= "@+id/text" -Android:layout_width= "Fill_parent" theandroid:layout_height= "Wrap_content" -/> -</LinearLayout>

When we enter the input box is not an integer number, we will immediately pop-up the input box, prompting you to correct

In Logcat view the order in which these methods are called:

Beforetextchanged-->ontextchanged-->ontextchanged

The second example implements the function of how many characters can be entered in a hint text box.

1 Package Com.lingdududu.test; 2      3 import android.app.Activity; 4 import Android.os.Bundle; 5 import android.text.Editable; 6 import Android.text.TextWatcher; 7 import Android.view.View; 8 import Android.widget.Button; 9 import Android.widget.EditText; Ten import Android.widget.TextView;  One       A Public class Mainactivity extends Activity { - private Button clearbtn;  - private EditText et;  the private TextView TV;  -FinalintMax_length = 20;  -      intRest_length =max_length;  - @Override +PublicvoidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate);  + Setcontentview (R.layout.main);  ATV =(TextView) Findviewbyid (r.id.tv);  atET =(EditText) Findviewbyid (r.id.et);  -                -CLEARBTN =(Button) Findviewbyid (R.ID.BTN);  -       -Et.addtextchangedlistener (NewTextwatcher () { -                                in @Override -PublicvoidBeforetextchanged (Charsequence S,intStartintCount, to                         intAfter ) {   +Tv.settext ("can also input" +rest_length+ "words");  -                 }   the                                * @Override $Publicvoidaftertextchanged (Editable s) {Panax NotoginsengTv.settext ("can also input" +rest_length+ "words");  -                 }   the                    + @Override APublicvoidOnTextChanged (Charsequence S,intStartintBefore,intcount) {   the                     if(rest_length>0){   +Rest_length = Max_length-et.gettext (). Length ();  -                     }   $                 }              $             });  -              -Clearbtn.setonclicklistener (NewButton.onclicklistener () { the @Override -PublicvoidOnClick (View v) {WuyiEt.settext ("");  theRest_length =max_length;  -                 }   Wu             });  -         }   About}

This article is from the "It Drip" blog, please be sure to keep this source http://liangruijun.blog.51cto.com/3061169/729505

"Android Advanced Learning" monitor edittext changes

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.