My Android step-by-step tour------>android a preliminary understanding of Textwatcher
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:24px">First look at the source code of Textwatcher</span></p></p><p><p><span style="font-size:24px"></span></p></p><pre name="code" class="java">Package android.text;/** * If an object of a type was attached to an Editable, its methods would * be called when the text Is Changed. */public interface Textwatcher extends Nocopyspan {/** * This method was called to notify i, within <code >s</code>, * The <code>count</code> characters beginning at <code>start</code> * is about to is replaced by new text with length <code>after</code>. * It is a error to attempt-changes to <code>s</code> from * this callback. */public void beforetextchanged (charsequence s, int. start, int count, int after); /** * This method was called to notify you, within <code>s</code>, * the <code>count</co De> characters beginning at <code>start</code> * has just replaced old text that had length <CODE&G T;before</code>. * It is a error to attempt to make ChanGES to <code>s</code> from * this callback. */public void ontextchanged (charsequence s, int start, int before, int count); /** * This method was called to notify you, somewhere within * <code>s</code>, the text has been C Hanged. * It's legitimate to make further changes to <code>s</code> from * This callback, but being careful not to G Et yourself into a infinite * loop, because any changes your make would cause this method to be * called again RECU Rsively. * (you aren't told where the took place because other * aftertextchanged () methods may already has made other Changes * and invalidated the Offsets. But if you need to know here, * can use {@link spannable#setspan} in {@link #onTextChanged} * to mark your PLA Ce and then look up from here where the span * ended up. */public void aftertextchanged (Editable s);}</pre><p><p></p></p><p><p><span style="font-size:24px">The following uses a small example to learn about the use of Textwatcher</span></p></p><p><p><span style="font-size:24px">Implement the interface</span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Textwatcher mtextwatcher = new Textwatcher () {private charsequence temp;private int editstart;private int editEnd; @Overri depublic void beforetextchanged (charsequence s, int arg1, int arg2,int arg3) {temp = s;} @Overridepublic void ontextchanged (charsequence s, int arg1, int arg2, int arg3) {mdetailview.settext (s);} @Overridepublic void aftertextchanged (Editable s) {editstart = medittext.getselectionstart (); editend = Medittext.getselectionend (), if (temp.length () > One) {toast.maketext (mainactivity.this), "the number of words you have entered has exceeded the limit!" ", toast.length_short). show (); s.delete (editStart-1, editend); int tempselection = Editstart;medittext.settext (s); Medittext.setselection (tempselection);}};</pre></pre>Register this monitor Again.<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> TextView mdetailview; EditText medittext; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mdetailview = (TextView) Findviewbyid (r.id.mdetailview); mEditText = ( EditText) Findviewbyid (r.id.medittext); Medittext.addtextchangedlistener (mtextwatcher);}</pre></pre><br>See the effect of running<p><p></p></p><p><p><br><br></p></p><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px"></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><span style="font-size:18px">==================================================================================================</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><strong><span style="font-size:18px">Ouyangpeng welcome reprint, sharing with people is the source of progress!</span></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><strong><span style="font-size:18px">Reprint please keep the original address <span style="color:rgb(153,51,153)">: Http://blog.csdn.net/ouyang_peng</span></span></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,51); text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><span style="font-size:18px">==================================================================================================</span></span> <span style="color:rgb(85,85,85); font-family:‘microsoft yahei‘; line-height:35px; font-size:12px; background-color:rgb(248,248,248)"> </span></p></p><br><br><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, welcome reprint, reprint Please indicate source Http://blog.csdn.net/ouyang_peng</p></p> <p><p>My Android step-by-step tour------>android a preliminary understanding of Textwatcher</p></p></span>
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