Xamarin.Android开发实践(一)
2015-07-25 09:28
501 查看
原文:Xamarin.Android开发实践(一)
3.然后我们需要给translateButton绑定监听事件,判断输入的字符是否为有效的电话号码,如果是则启用CallButton否则不启用,同时还修改CallButton的文字(在上面代码后面追加):
4.最后我们需要绑定callButton的监听事件,以便能够在用户点击后弹出对话框确认用户是否需要拨打,并拨打电话(依然是接着上面的追加):
一、准备工作
1.创建一个空的解决方案,并命名为Phoneword1 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),当然速度跟你的真机一样。
相关文章推荐
- Android中的多线程编程(一)附源码
- 在Android中可以使用Gson解析JSON数据
- Mac上Android studio环境搭建
- Android Scroller简单用法
- android 目前遇到的程序崩溃常见原因
- Android开发之利用jsoup制作简易的图书馆图书搜索客户端
- 用Xamarin 实现园友的 :Android浮动小球与开机自启动
- android framework 层源码分析常见方法
- android各种组件的监听器
- Android 权限大全
- Android中常用单位(dpi、px、dp、sp)
- Android 流量统计TrafficStats总结
- Android 单元测试
- Android SQLite数据库存储实现
- Android文件操作
- Android Media 音乐属性
- Android them 的使用和属性
- android keyevent整理
- Android Drawable Resource学习(十三)、ShapeDrawable
- Android Drawable Resource学习(十二)、GradientDrawable