同时修改Button控件的id和text:
1 namespace Phoneword_Droid 2 { 3 public static class PhoneTranslator 4 { 5 public static string ToNumber(string raw) 6 { 7 if (string.IsNullOrWhiteSpace(raw)) 8 return ""; 9 else 10 raw = raw.ToUpperInvariant(); 11 12 var newNumber = new StringBuilder(); 13 foreach (var c in raw) 14 { 15 if (" -0123456789".Contains(c)) 16 newNumber.Append(c); 17 else 18 { 19 var result = TranslateToNumber(c); 20 if (result != null) 21 newNumber.Append(result); 22 } 23 } 24 return newNumber.ToString(); 25 } 26 27 public static int? TranslateToNumber(char c) 28 { 29 if ("ABC".Contains(c)) 30 return 2; 31 else if ("DEF".Contains(c)) 32 return 3; 33 else if ("GHI".Contains(c)) 34 return 4; 35 else if ("JKL".Contains(c)) 36 return 5; 37 else if ("MNO".Contains(c)) 38 return 6; 39 else if ("PQRS".Contains(c)) 40 return 7; 41 else if ("TUV".Contains(c)) 42 return 8; 43 else if ("WXYZ".Contains(c)) 44 return 9; 45 return null; 46 } 47 } 48 }
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;
1 string translatedNumber = string.Empty; 2 translateButton.Click += (object sender, EventArgs e) => 3 { 4 translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text); 5 if (String.IsNullOrWhiteSpace(translatedNumber)) 6 { 7 callButton.Text = "Call"; 8 callButton.Enabled = false; 9 } 10 else 11 { 12 callButton.Text = "Call" + translatedNumber; 13 callButton.Enabled = true; 14 } 15 };
1 callButton.Click += (s, e) => 2 { 3 //对话框 4 var callDialog = new AlertDialog.Builder(this); 5 6 //对话框内容 7 callDialog.SetMessage("Call" + translatedNumber + "?"); 8 9 //拨打按钮 10 callDialog.SetNeutralButton("Call", delegate 11 { 12 //使用意图拨打电话 13 var callIntent = new Intent(Intent.ActionCall); 14 15 //将需要拨打的电话设置为意图的参数 16 callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); 17 18 StartActivity(callIntent); 19 }); 20 21 //取消按钮 22 callDialog.SetNegativeButton("Cancel", delegate { }); 23 24 //显示对话框 25 callDialog.Show(); 26 };
续篇:多界面
Xamarin.Android快速入门,布布扣,bubuko.com
原文:http://www.cnblogs.com/yaozhenfa/p/xamarin_android_quickstart.html