Android implements IP address input for imitation computers

Source: Internet
Author: User

The need has recently been changed to a special,It 's estimated that some people have come across this disgusting demand.,The client actually needs to enterIPAddress,I feel a little ripped .,Customer InputIPAddress,hehe,anyway,No, it's not done.,didn't cut the manager's strength.,only the code..

The following start the subject , First run the effect , easy to watch



The input box above is written as a custom combo control,Easy to use,It's pretty easy to say .,The main combination of fourEditText,then JudgeEditTextInput Content,There is also the focus of the acquisition is more important,as I just learnedAndroidsoon,there might be a better way.,This is just a way of writing my own implementation.,the same way you can implement a phone number in this manner,bank card and other input methods

Paste the code for the custom combo control below :

Package Com.thea.guo.view;import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.text.editable;import Android.text.TextUtils;import Android.text.textwatcher;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.edittext;import Android.widget.linearlayout;import android.widget.Toast; Import com.thea.guo.r;/** * Custom control implementation IP address special input * * @author sub-ink * * 2015-1-4 */public class Ipedittext extends Linear Layout {private EditText mfirstip;private EditText msecondip;private EditText mthirdip;private EditText mfourthip; private string Mtext;private string Mtext1;private string mtext2;private string mtext3;private string Mtext4;private Shar Edpreferences Mpreferences;public Ipedittext (context context, AttributeSet Attrs) {Super (context, attrs);/** * Initialize control */ View view = Layoutinflater.from (context). Inflate (R.layout.custom_my_edittext, this); Mfirstip = (EditText) Findviewbyid (r.id.ip_first), Msecondip = (EditText) Findviewbyid (r.id.ip_second); Mthirdip = (EditText) Findviewbyid (r.id.ip_third); mfourthip = (EditText) Findviewbyid (R.id.ip_fourth); mpreferences = Context.getsharedpreferences ("Config_ip", context.mode_private); Operatingedittext (Context);} /** * Get content in EditText, when each edittext character reaches three bits, automatically jumps to the next edittext when the user clicks. * Next edittext get focus */private void Operatingedittext (Final context context) {Mfirstip.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged (charsequence s, int start, int Before,int count) {/** * gets editte input, makes a judgment, if greater than 255, the hint is not valid, when the number is valid for three digits the next edittext gets focus, * the user clicks on AH. When the next edittext gets focus */if (s! = Null && s.length () > 0) {if (S.length () > 2 | | s.tostring (). Trim (). Contains (".")) {if (s.tostring (). Trim (). Contains (".")) {mText1 = s.tostring (). substring (0, s.length ()-1); Mfirstip.settext (MTEXT1);} else {mText1 = s.tostring (). Trim (); if (Integer.parseint (MTEXT1) > 255) {toast.maketext (context, "Please enter a valid IP address", ToaSt. Length_long). Show (); return;} Editor editor = Mpreferences.edit (); Editor.putint ("Ip_first", Mtext1.length ()); Editor.commit (); Msecondip.setfocusable (True); Msecondip.requestfocus ();}}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Aftertextch Anged (Editable s) {}}) Msecondip.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged ( charsequence s, int start, int before,int count) {/** * get editte input, make judgment, if greater than 255, prompt is not valid, when number is valid for three digits next edittext get focus, * user Click Yes . When the next edittext gets focus */if (s! = null && s.length () > 0) {if (S.length () > 2 | | s.tostring (). Trim (). Contains (". ")) {if (s.tostring (). Trim (). Contains (". ")) {mText2 = s.tostring (). substring (0, s.length ()-1); Msecondip.settext (MTEXT2);} else {mText2 = s.tostring (). Trim (); if (Integer.parseint (MTEXT2) > 255) {toast.maketext (context, "Please enter a valid IP address", Toast.length_long). Show (); return;} Editor editor = Mpreferences.edit (); Editor.putint ("Ip_second", Mtext2.leNgth ()); Editor.commit (); mthirdip.setfocusable (true); Mthirdip.requestfocus ();}} /** * When the user needs to delete, at this time the EditText is empty, the previous edittext get focus */if (start = = 0 && s.length () = = 0) {mfirstip.setfocusable (true); m Firstip.requestfocus (); Mfirstip.setselection (Mpreferences.getint ("Ip_first", 0);}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Aftertextch Anged (Editable s) {}}) Mthirdip.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged ( charsequence s, int start, int before,int count) {/** * get editte input, make judgment, if greater than 255, prompt is not valid, when number is valid for three digits next edittext get focus, * user Click Yes . When the next edittext gets focus */if (s! = null && s.length () > 0) {if (S.length () > 2 | | s.tostring (). Trim (). Contains (". ")) {if (s.tostring (). Trim (). Contains (". ")) {mText3 = s.tostring (). substring (0, s.length ()-1); Mthirdip.settext (MTEXT3);} else {mText3 = s.tostring (). Trim (); if (Integer.parseint (MTEXT3) > 255) {toast.maketext (context, "Please enter a valid IP address", Toast.lenGth_long). Show (); return;} Editor editor = Mpreferences.edit (); Editor.putint ("Ip_third", Mtext3.length ()); Editor.commit (); Mfourthip.setfocusable (True); Mfourthip.requestfocus ();}} /** * When the user needs to delete, at this time the EditText is empty, the previous edittext get focus */if (start = = 0 && s.length () = = 0) {msecondip.setfocusable (true); Msecondip.requestfocus (); Msecondip.setselection (Mpreferences.getint ("Ip_second", 0);}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Aftertextch Anged (Editable s) {}}) Mfourthip.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged ( charsequence s, int start, int before,int count) {/** * get editte input, make judgment, if greater than 255, prompt is not valid, when number is valid for three digits next edittext get focus, * user Click Yes . When the next edittext gets focus */if (s! = null && s.length () > 0) {mText4 = S.tostring (). Trim (); if (Integer.parseint (MTEXT4 > 255) {toast.maketext (context, "Please enter a valid IP address", Toast.length_long). Show (); return;} Editor editor = Mpreferences.edit (); Editor.putint ("Ip_fouRTH ", Mtext4.length ()); Editor.commit ();} /** * When the user needs to delete, at this time the EditText is empty, the previous edittext get focus */if (start = = 0 && s.length () = = 0) {msecondip.setfocusable (true); Msecondip.requestfocus (); Msecondip.setselection (Mpreferences.getint ("Ip_third", 0);}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Aftertextch Anged (Editable s) {}});} Public String GetText (context context) {if (Textutils.isempty (MTEXT1) | | Textutils.isempty (MTEXT2) | | Textutils.isempty (MTEXT3) | | Textutils.isempty (MTEXT4)) {Toast.maketext (context, "Please enter a valid IP address", Toast.length_long). Show (); return MText1 + "." + MText2 + "." + MText3 + "." + MText4;}}

The comments in the code are explained in detail , What are the questions ? or you have a better way of doing it. , come on, big god . , Communicate together , Progress together !


CSDN Code: http://download.csdn.net/detail/elinavampire/8331451


GitHub Managed Address: http://download.csdn.net/detail/elinavampire/8331451


Recently added a lot of great God's group, many exchanges have a quick ascension, Thanksgiving all.


Android implements IP address input for imitation computers

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.