I. Preparatory work 1. Create an empty solution and name it Phoneword
2. Right-click on the solution new-new project and named Phoneword_droid
Ii. interface 1. Open Resources Folder-"Layout folder double-click Open Main.axml
2. The following interface will then appear
3. We then select the button and delete (press DELETE) and drag a Text (Large) control from the left toolbox to the interface as follows:
4. Also modify the value of text through the Properties window:
5. Drag a plain text control immediately below the text control and modify the Text property to 1-855-xamarin. Then drag a button control to the bottom of the plain text control:
Modify the ID and text of the button control at the same time:
6. Then drag a button control below the Translatebutton control and set the ID property to @+id/callbutton and text to call, and the final effect is as follows:
Third, code 1. Right-click the project, add-new item, select the class, and set the class name to Phonetranslator, and write the following code in it (the key function of the code is to convert the string to the correct format of the phone number):
1 namespacephoneword_droid2 {3 Public Static classPhonetranslator4 {5 Public Static stringTonumber (stringRaw)6 {7 if(string. Isnullorwhitespace (Raw))8 return "";9 ElseTenRaw =Raw. Toupperinvariant (); One A varNewnumber =NewStringBuilder (); - foreach(varCinchRaw) - { the if("-0123456789". Contains (c)) - Newnumber.append (c); - Else - { + varresult =Translatetonumber (c); - if(Result! =NULL) + newnumber.append (result); A } at } - returnnewnumber.tostring (); - } - - Public Static int? Translatetonumber (Charc) - { in if("ABC". Contains (c)) - return 2; to Else if("DEF". Contains (c)) + return 3; - Else if("GHI". Contains (c)) the return 4; * Else if("JKL". Contains (c)) $ return 5;Panax Notoginseng Else if("MNO". Contains (c)) - return 6; the Else if("PQRS". Contains (c)) + return 7; A Else if("TUV". Contains (c)) the return 8; + Else if("WXYZ". Contains (c)) - return 9; $ return NULL; $ } - } -}View Code
Then we open the MainActivity.cs file:
2. Then the first thing we need to do is get these controls through Findviewbyid, and the code in OnCreate looks like this (requires a successful build first, preventing resource.id from having a corresponding control ID):
1 base . OnCreate (bundle); 2 Setcontentview (Resource.Layout.Main); 3 EditText phonenumbertext = Findviewbyid<edittext> (Resource.Id.PhoneNumberText); 4 button Translatebutton = Findviewbyid<button> (Resource.Id.TranslateButton); 5 button Callbutton = Findviewbyid<button> (Resource.Id.CallButton); 6 7 callbutton.enabled = false ;
View Code
3. Then we need to bind the Translatebutton to listen to the event, determine whether the input character is a valid phone number, if it is enabled Callbutton otherwise not enabled, but also modify the Callbutton text (appended to the above code):
1 stringTranslatednumber =string. Empty;2Translatebutton.click + = (Objectsender, EventArgs e) = =3 {4Translatednumber =Phonetranslator.tonumber (phonenumbertext.text);5 if(String.isnullorwhitespace (translatednumber))6 {7Callbutton.text ="Pager";8callbutton.enabled =false;9 }Ten Else One { ACallbutton.text ="Pager"+Translatednumber; -callbutton.enabled =true; - } the};View Code
4. Finally, we need to bind the Callbutton listener event so that the user clicks on the dialog box to confirm that the user needs to make a call and calls (still appended above):
1Callbutton.click + = (s, e) = =2 {3 //dialog Box4 varCalldialog =NewAlertdialog.builder ( This);5 6 //dialog box Contents7Calldialog.setmessage ("Pager"+ Translatednumber +"?");8 9 //Dial ButtonTenCalldialog.setneutralbutton ("Pager",Delegate One { A //use intent to make a phone call - varCallintent =NewIntent (intent.actioncall); - the //set the number of calls you want to make to an intent parameter -Callintent.setdata (Android.Net.Uri.Parse ("Tel:"+translatednumber)); - - startactivity (callintent); + }); - + //Cancel Button ACalldialog.setnegativebutton ("Cancel",Delegate { }); at - //Show dialog Box - calldialog.show (); -};View CodeFour, the operation
The author here is x86 under the simulator, not arm, about how to open x86 under the simulator needs people can leave a message (need your CPU support Vt-x), of course, speed and your real machine.
Sequel: Multi-Interface