您的位置:首页 > 移动开发 > Android开发

Xamarin.Android开发实践(一)

2015-07-25 09:28 501 查看
原文:Xamarin.Android开发实践(一)

一、准备工作

1.创建一个空的解决方案,并命名为Phoneword

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;


3.然后我们需要给translateButton绑定监听事件,判断输入的字符是否为有效的电话号码,如果是则启用CallButton否则不启用,同时还修改CallButton的文字(在上面代码后面追加):

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             };


4.最后我们需要绑定callButton的监听事件,以便能够在用户点击后弹出对话框确认用户是否需要拨打,并拨打电话(依然是接着上面的追加):

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             };


四、运行

还需要添加对应的权限







笔者这里采用的是x86下的模拟机,并不是ARM下,关于如何开启x86下的模拟机需要的人可以留言(需要你的CPU支持VT-X),当然速度跟你的真机一样。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: