First, the preparatory work
1. Create a new project named Phoneword
2. After the project is created, let's expand theResourcesfolder and find and open the "Layout" folder under that folder, double-click Main.axml to open it in the Android designer, this is our layout file.
3. Let's Select "Hello World, click me" button on the design surface and press the DELETE key to delete it. From the Toolbox (the area on the left), enter textin the search field and drag a text (Large) control to the area at the center of the design surface:
4. Select we drag in the control, we can modify the input text in the Properties window
5. Then we drag a simple text control into the designer and place it under the controls we just added
6. In the designer, select the control you just added, we can change its ID and text in the Properties window
7. Let's Drag a "button" to the designer and change its ID and text
8. Then we drag a button under the button just now and set its properties, then ctrl+s Save it
Second, the Code
1. Now let's add some code, right click on the project, select "Add New Item" Add a C # code file named PhoneTranslator.cs
2. Copy the following code in
1 namespacePhoneword2 {3 Public Static classPhonewordtranslator4 {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 //Otherwise we ' ve skipped a non-numeric char - } - returnnewnumber.tostring (); - } - Static BOOLContains ( This stringKeyString,Charc) - { in returnKeystring.indexof (c) >=0; - } to Static int? Translatetonumber (Charc) + { - if("ABC". Contains (c)) the return 2; * Else if("DEF". Contains (c)) $ return 3;Panax Notoginseng Else if("GHI". Contains (c)) - return 4; the Else if("JKL". Contains (c)) + return 5; A Else if("MNO". Contains (c)) the return 6; + Else if("PQRS". Contains (c)) - return 7; $ Else if("TUV". Contains (c)) $ return 8; - Else if("WXYZ". Contains (c)) - return 9; the return NULL; - }Wuyi } the}View Code
3. Next, we will add code to connect the user. Let's double-click the Mainactivity class in the project to open it (that is, the activity in Android).
4. Locate the OnCreate method in the Mainactivity class, delete the code in the template, and we will write the code in Oncrate
5. Below we need to find the controls we added in the designer, let's add the following code to the OnCreate method (after the Setcontentview method).
1 EditText Phonenumbertext = findviewbyid<edittext>(Resource.Id.PhoneNumberText); 2 Button Translatebutton = findviewbyid<button>(Resource.Id.TranslateButton); 3 Button Callbutton = findviewbyid<button> (Resource.Id.CallButton);
View Code
6. Below we add the code for the Translate button Click event after the previous step
//set Callbutton to not pointcallbutton.enabled =false; //set Translatednumber to empty stringTranslatednumber =string. Empty; //Add a Translatebutton click eventTranslatebutton.click + = (Objectsender, EventArgs e) = = { //convert a user's phone number to a numberTranslatednumber =Phonewordtranslator.tonumber (Phonenumbertext.text); if(String.isnullorwhitespace (Translatednumber))//If there is a blank or a space{Callbutton.text="Pager"; Callbutton.enabled=false; } Else{Callbutton.text="Pager"+Translatednumber; Callbutton.enabled=true; } };View Code
7. Then add the code for the call butto Click event
//Set the Callbutton click eventCallbutton.click + = (Objectsender, EventArgs e) = = { //try to call after the button is clicked varCalldialog =NewAlertdialog.builder ( This); Calldialog.setmessage ("Pager"+ Translatednumber +"?"); Calldialog.setneutralbutton ("Pager",Delegate { //Create a call event varCallintent =NewIntent (Intent.actioncall); Callintent.setdata (Android.Net.Uri.Parse ("Tel:"+translatednumber)); StartActivity (callintent); }); Calldialog.setnegativebutton ("Cancel",Delegate { }); //pop-up prompt boxcalldialog.show (); };View Code
8. Finally, let's add a call to the app: right-click on project Properties Android Manifest tick call_phone
9. Right-click on the solution, rebuild the solution, if no error indicates that our program has been completed, click the Run button to run, the final run effect.
Xamarin.android Getting Started: Xamarin Quick Start