Android implements ip address input for computer imitation, and android implements ip imitation

Source: Internet
Author: User

Android implements ip address input for computer imitation, and android implements ip imitation

Recently, this demand has been changed to a special one. It is estimated that some people have encountered this disgusting requirement. The client needs to enter an IP address, which is a bit confusing. The customer enters the IP address by himself, I can't do it without cutting the manager's strength. I can only hack the code.

Next, let's start with the question. First, let's get the running effect for easy viewing.



The input box above is written as a custom Composite Control, which is easy to use. The function is also quite simple. It mainly combines four edittexts and then judges the EditText input content, in addition, it is important to obtain the focus. As I have just learned Android, there may be a better way. Here I just want to write my own implementation method, similarly, you can use this method to provide telephone numbers, bank cards, and other input methods.

The following is the code of the custom Composite Control:

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; impor T android. widget. toast; import com. thea. guo. r;/*** custom control implements special IP address input ** @ author sub-ink ** 2015-1-4 */public class IPEditText extends LinearLayout {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 SharedPreferences mPreferences; pub Lic IPEditText (Context context, AttributeSet attrs) {super (context, attrs);/*** initialization 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. getS HaredPreferences ("config_IP", Context. MODE_PRIVATE); OperatingEditText (context);}/*** get the content in EditText. When each Edittext character reaches three, it automatically jumps to the next EditText. When the user clicks. * The next EditText gets the focus */private void OperatingEditText (final Context context) {mFirstIP. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {/*** obtains the EditTe input content for judgment, if the value is greater than 255, the message is invalid. If the number is valid The next EditText gets the focus. * when the user clicks, the next EditText gets the 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, "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 afterTextChanged (Editable s ){}}); mSecondIP. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {/*** obtain the EditTe input and determine If the value is greater than 255, an invalid message is displayed. When the number is valid, three digits are used for the next EditText to obtain the focus. * when the user clicks., the next EditText gets the 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, "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, when the EditText is empty, the previous EditText gets the focus */if (start = 0 & s. length () = 0) {mFirstIP. setFocusable (true); mFirstIP. requestFocus (); mFirstIP. setSelection (mPreferences. getInt ("IP_FIRST", 0) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic vo Id afterTextChanged (Editable s) {}}); mThirdIP. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {/*** obtains the EditTe input content for judgment, if the value is greater than 255, the error message is invalid. When the number is valid, three digits are followed by an EditText to obtain the focus. * click here. the next EditText gets the 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, "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, when the EditText is empty, the previous EditText gets the 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) {}@ Overridepubli C void afterTextChanged (Editable s) {}}); mFourthIP. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {/*** obtains the EditTe input content for judgment, if the value is greater than 255, the error message is invalid. When the number is valid, three digits are followed by an EditText to obtain the focus. * click here. the next EditText gets the focus */if (s! = Null & s. length ()> 0) {mText4 = s. toString (). trim (); if (Integer. parseInt (mText4)> 255) {Toast. makeText (context, "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, when the EditText is empty, the previous EditText gets the 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 afterTextChanged (Editable s) {}});} public String getText (Context context) {if (TextUtils. isEmpty (mText1) | TextUtils. isEmpty (mText2) | TextUtils. isEmpty (mText3) | TextUtils. isEmpty (mText4) {Toast. makeText (context, "enter a valid IP Address", Toast. LENGTH_LONG ). show ();} return mText1 + ". "+ mText2 + ". "+ mText3 + ". "+ mText4 ;}}

The annotations in the Code are explained in detail. Please leave a message if you have any questions! Or you have a better implementation method. Please contact us and make progress together!


Csdn code: http://download.csdn.net/detail/elinavampire/8331451


Github hosting address: http://download.csdn.net/detail/elinavampire/8331451


Recently, many groups of great gods have been added, and many exchanges have quickly improved. Thanks to everything ..


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.