Android dialing number judgment

Source: Internet
Author: User

Add the call permission to androidmanifest

<Uses-Permission Android: Name = "android. Permission. call_phone"/>

Public class Boda extends activity {/* declares the name of the button and edittext objects */private button mbutton1; private edittext medittext1;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);/* use the findviewbyid constructor to construct the edittext and button objects */medittext1 = (edittext) findviewbyid (R. id. myedittext1); mbutton1 = (Button) findviewbyid (R. id. mybutton1);/* set the onclicklistener of the button object to listen to The onclick event */mbutton1.setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {try {/* Get the user input string in edittext */string strinput = medittext1.gettext (). tostring (); If (isphonenumbervalid (strinput) = true) {/* construct a new intent to run the action. the constant of call and the use of URI to bring the string into */intent myintentdial = new intent ("android. inte NT. action. call ", Uri. parse ("Tel:" + strinput);/* Add a custom intent object to the startactivity () method to run the call */startactivity (myintentdial ); medittext1.settext ("");} else {medittext1.settext (""); toast. maketext (BODA. this, "The entered phone format does not match", toast. length_long ). show () ;}} catch (exception e) {e. printstacktrace () ;}}) ;}/ * check whether the string is a telephone number and return the true or false value */public static Boolean isphonenumbervalid (string phone Number) {Boolean isvalid = false;/* acceptable phone numbers: * ^ \\(?: You can start with "(" * (\ D {3}): followed by three numbers *\\)?: You can use ")" to connect * [-]?: After the preceding format, you can select "-". * (\ D {4}): followed by three numbers * [-]?: You can choose "-" to connect the device. * (\ D {4}) $: ends with four digits. * The following numeric formats can be compared: * (123) 456-78900,123-4560-7890,123 45678900, (123)-4560-7890 */string expression = "^ \\(? (\ D {3 })\\)? [-]? (\ D {3}) [-]? (\ D {5}) $ "; string expression2 =" ^ \\(? (\ D {3 })\\)? [-]? (\ D {4}) [-]? (\ D {4}) $ "; charsequence inputstr = phonenumber;/* create pattern */pattern = pattern. compile (expression);/* pass pattern as a parameter to matcher for regular expression */matcher = pattern. matcher (inputstr);/* Create pattern2 */pattern pattern2 = pattern. compile (expression2);/* pass pattern2 as a parameter to matcher2 for regular expression */matcher matcher2 = pattern2.matcher (inputstr); If (matcher. matches () | matcher2.matches () {isvalid = true;} return isvalid ;}}

Go to the dialing page

Myimagebutton = (imagebutton) findviewbyid (R. id. myimagebutton); myimagebutton. setonclicklistener (New imagebutton. onclicklistener () {@ override public void onclick (view v) {/* Call dialing screen */intent myintentdial = new intent ("android. intent. action. call_button "); startactivity (myintentdial );}});

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.