Android EditText Input Detection

Source: Internet
Author: User
<span id="Label3"></p><p><p>When a feature is recently developed, a story occurs with the following plot:</p></p><p><p>The function is not complex, which requires a edittext to get the information entered by the User. so, I made a dialog to show my input interface (code below):</p></p><p><p></p></p><pre name="code" class="java">Malertdialog = new Alertdialog.builder (this)//, android. R.style.theme_holo_light. SetIcon (r.drawable.ic_dialog_info_light). settitle (r.string.model_ rename_device). setview (createdialogview (devicename)). Setpositivebutton (android. r.string.ok, new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int Which) {//string devicename = mdevicenameview.gettext (). toString (); reset_success=false; Reset_model_name (mdevicenameview.gettext (). toString ());//finish (); }}). Setnegativebutton (android. r.string.cancel, new Dialoginterface.onclicklistener () {public void OnClick (Dia Loginterface dialog, int Which) {//finish (); } }). setondismisslistener (new dialoginterface.ondismisslistener () {@Overridepublic void Ondismiss (dialoginterface arg 0) {if (reset_success) {start_confir_resetphone ();} Else{finish ();}}) . Create (); Malertdialog.getwindow (). setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE ); Malertdialog.show ();</pre><br><pre name="code" class="java"> Private View Createdialogview (String devicename) {final layoutinflater layoutinflater = (layoutinflater) This . Getsystemservice (context.layout_inflater_service); View view = Layoutinflater.inflate (r.layout.dialog_edittext, null); Mdevicenameview = (EditText) View.findviewbyid (r.id.edittext); Mdevicenameview.setfilters (new inputfilter[] {new utf8bytelengthfilter (model_name_max_length_bytes) }); Mdevicenameview.settext (devicename); Set initial value before adding listener Mdevicenameview.addtextchangedlistener (this); Mdevicenameview.setoneditoractionlistener (new Textview.oneditoractionlistener () {@Override public b Oolean oneditoraction (TextView v, int actionid, keyevent event) {if (actionid = = Editorinfo.ime_action_don E) {reset_model_name (v.gettext (). toString ()); Malertdialog.dismiss ();//finish (); RetUrn true; Action handled} Else {//finish (); Return false; Not handled}}); mdevicenameview.setselection (mdevicenameview.length ()); Return view; }</pre>It's easy to implement! but when I store the user input string, the problem comes!<p><p></p></p><p><p>Originally the user input string needs to be stored in a section of our own configuration of nv, it is important to give me the space stored in a string of only 20 byte, there is no error is a Byte. So it is easy to enter a character that cannot exceed 20 characters, because it is a byte type, so for the input character must be detected, the character must be in a byte value space (0-127) Inside. actually, it's the ASIC Code.</p></p><p><p>therefore, the input character detection is Required.</p></p><p><p>In order to be able to detect edittext input characters in real time you need to edittext.addtextchangedlistener () to add a textwatcher detector and implement the Method:</p></p><p><p>public void aftertextchanged (Editable S)</p></p><p><p>public void beforetextchanged (charsequence s, int start, int count, Int. After)</p></p><p><p>public void ontextchanged (charsequence s, int start, int before, int Count)</p></p><p><p>The first thing to think of is the Aftertextchanged method to determine whether the current input character is the correct character, if not by editable s to change: S.delete () to Delete. or just use this edittext to start typing the characters from the new Setting. : SetText ();</p></p><p><p>In fact, these two methods are not, when the fast input of the wrong characters will appear abnormal, it is obvious that the problem of Synchronization.</p></p><p><p>Soon i'll give you another solution: in ontextchanged () detects if there are any unusual characters, and if any, it is handled by sending a message in the form of handler.</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public void ontextchanged (charsequence s, int. start, int before, int Count) {for (int i=0; i < count; i++) {if (s.charat (i +start) > 127 | | S.charat (i+start) < 0) {Message msg = mhandler.obtainmessage (handlemessage_detection_mg); msg.obj = s; Mhandler.sendmessage (msg); break;}} LOG.D (debug_str, "ontextchanged str=" +s.tostring () + "start=" +start+ "; before=" +before+ "; count=" +count);}</pre></pre><br><pre name="code" class="java"><pre name="code" class="java"> Handler Mhandler = new Handler () {public void handlemessage (Message msg) {switch (msg.what) {case Handlemessage_detec Tion_mg:inpttext_error_correction ((charsequence) msg.obj); break;case handlemessage_blue_name_mg:inpttext_rename_ Blue (String) msg.obj; break;default:break;}} ;</pre></pre><p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">private void Inpttext_error_correction (charsequence chars) {if (chars! = null) {StringBuilder str_b = new StringBuilder ( chars); char temp;int start_indx = -1;for (int i = 0; i < str_b.length (); I++) {temp = Str_b.charat (i); if (temp > 127 | | Temp < 0) {if (start_indx < 0) {start_indx = i;} Str_b.deletecharat (i);}} Mdevicenameview.settext (str_b.tostring ()); If (start_indx < 0) {start_indx = Mdevicenameview.length ();} Mdevicenameview.setselection (start_indx); Toast.maketext (rename_model_activity.this, getString (r.string.set_name_error_character_notice), Toast.LENGTH_ Short). Show ();}}</pre></pre><br><br>finally, the restrictions on the input characters can be configured by Edittext.setfilters ():<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> Mdevicenameview.setfilters (new inputfilter[] { new Utf8bytelengthfilter (model_name_max_length_bytes) });</pre></pre><br><pre name="code" class="java"><pre name="code" class="java"></pre></pre>For an input error character, the byte returns "" to:<p><p></p></p><p><p>for (int i = start; i < end; i++) {<br>char C = Source.charat (i);<br></p></p><p><p>If (c > 127 | | C < 0) {<br>Return "";<br>}</p></p><p><p>}<br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>Android EditText Input Detection</p></p></span>

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.