Android synchronous listening input box and regular expression of phone number!

Source: Internet
Author: User

The following program implements the following three functions:

 

1. synchronous listening for changes in text input boxes. By default, the following three methods are inherited;
2. Use regular expressions to determine whether the entered phone number is legal;
3. After the activity is displayed for one second, the keyboard jumps out;

 

Package COM. my; </P> <p> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import Java. util. regEx. matcher; <br/> Import Java. util. regEx. pattern; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. text. editable; <br/> Import android. text. textwatcher; <br/> Import android. util. log; <br/> Import android. view. inputmethod. input Methodmanager; <br/> Import android. widget. button; <br/> Import android. widget. edittext; </P> <p> public class logincheckactivity extends activity <br/>{< br/> private button checknextbutton = NULL; <br/> private button checkcancelbutton = NULL; <br/> private edittext phoneedittext = NULL; <br/> private textwatcher phonetextwatcher; <br/> private string phonenumber = NULL; <br/> private inputmethodmanager im M; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) <br/> {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. login_check); <br/> initview (); <br/>}</P> <p> private void initview () <br/> {<br/> // todo auto-generated method stub <br/> checknextbutton = (button) findviewbyid (R. id. button_check_next); <br/> checkcancelbutton = (Button) findviewbyid (R. id. button_check_cancel); <br/> phoneedittext = (edittext) findviewbyid (R. id. login_check_edittext); <br/> checknextbutton. setenabled (false ); // set the button to disabled </P> <p>/** <br/> * check whether the mobile phone number entered by the listener is correct <br/> */<br/> phonetextwatcher = new textwatcher () <br/>{</P> <p> @ override <br/> Public void aftertextchanged (editable S) <br/>{< br/> // todo auto-generated method stub <br/>}</P> <p> @ override <br/> Public void beforetextchanged (charsequence S, int start, int count, <br/> int after) <br/>{< br/> // todo auto-generated method stub <br/>}</P> <p> @ override <br/> Public void ontextchanged (charsequence s, int start, int before, <br/> int count) <br/>{< br/> // todo auto-generated method stub <br/> phonenumber = phoneedittext. gettext (). tostring (); <br/> If (isphonenumbervalid (phonenumber) = true) <br/> {< Br/> checknextbutton. setenabled (true); <br/> log. E ("@", "ture"); <br/>}< br/> else <br/>{< br/> checknextbutton. setenabled (false); <br/> log. E ("@", "false"); <br/>}</P> <p >}; <br/> phoneedittext. addtextchangedlistener (phonetextwatcher); <br/>}</P> <p>/** <br/> * check whether the string is a telephone number, returns the true or false value. <br/> */<br/> Public static Boolean isphonenumbervalid (string phonenumber) <br/>{< br/> Boolean I Svalid = false; <br/>/** acceptable phone numbers: <br/> * ^ //(?: You can use "(" as the start <br/> * (// d {3}): followed by three numbers <br/> *//)?: You can use ")" connection <br/> * [-]?: After the preceding format, you can select "-" for the device "-". <br/> * (// d {3}): followed by three digits <br/> * [-]?: You can choose "-" to connect the device. <br/> * (// d {5}) $: end with five digits. <br/> * the following numeric formats can be compared: <br/> * (123) 456-7890,123-456-7890,123 4567890, (123) -456-7890 <br/> */<br/> string expression = "^ //(? (// D {3 })//)? [-]? (// D {3}) [-]? (// D {5}) $ "; </P> <p>/** acceptable phone numbers: <br/> * ^ //(?: You can use "(" as the start <br/> * (// d {3}): followed by three numbers <br/> *//)?: You can use ")" connection <br/> * [-]?: After the preceding format, you can select "-" for the device "-". <br/> * (// d {4}): followed by four digits <br/> * [-]?: You can choose "-" to connect the device. <br/> * (// d {4}) $: ends with four digits. <br/> * compare the following numeric formats: <br/> * (02) 3456-7890, 02-3456-7890,023 4567890, (02) -3456-7890 <br/> */<br/> string expression2 = "^ //(? (// D {3 })//)? [-]? (// D {4}) [-]? (// D {4}) $ "; </P> <p> charsequence inputstr = phonenumber; <br/>/* create pattern */<br/> pattern = pattern. compile (expression); <br/>/* pass pattern as a parameter to matcher for regular expression */<br/> matcher = pattern. matcher (inputstr); <br/>/* Create pattern2 */<br/> pattern pattern2 = pattern. compile (expression2); <br/>/* pass pattern2 to matcher2 as the regular expression */<br/> matcher matcher2 = pattern2.matcher (inputstr ); <br/> If (matcher. matches () // | matcher2.matches () <br/>{< br/> isvalid = true; <br/>}< br/> return isvalid; <br/>}</P> <p> @ override <br/> protected void onresume () <br/> {<br/> // todo auto-generated method stub <br/> super. onresume (); <br/> timer = new timer (); <br/> timer. schedule (New timertask () <br/>{</P> <p> @ override <br/> Public void run () <br/>{< br/> Imm = (inputmethodmanager) getsystemservice (context. input_method_service); <br/> Imm. showsoftinput (phoneedittext, 0); <br/>}</P> <p >}, 1000 ); <br/>}</P> <p >}< br/>

 

There is also a more concise method to determine the mobile phone number, as shown below:

 

/** <Br/> * determine whether the mobile phone number is correct. <br/> * The number segment is allocated as follows: <br/> * Mobile: 134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188, <br/> * China Unicom: 130, 131, 132, 152, 155, 156, 185, 186 <br/> * China Telecom: 133, 153, 180, 189, (1349 Weitong) <br/> */<br/> Public static Boolean ismobilenum (string mobiles) <br/> {<br/> pattern P = pattern <br/>. compile ("^ (13 [0-9]) | (15 [^ 4, // d]) | (18 [-9]) // d {8} $ "); <br/> matcher M = P. matcher (mobiles); <br/> return M. matches ();

 

OK!

 

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.