代码改变世界

Xamarin.Android 入门实例(3)之呼叫电话号码

2015-01-30 13:26  Dorisoy  阅读(664)  评论(0编辑  收藏  举报

1.Main.axml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:minWidth="25px"
 7     android:minHeight="25px">
 8     <TextView
 9         android:text="请输入电话号码:"
10         android:textAppearance="?android:attr/textAppearanceLarge"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:id="@+id/Tag" />
14     <EditText
15         android:text="13002929017"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:id="@+id/PhoneNumberText" />
19     <Button
20         android:text="转化"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:id="@+id/TranslateButton" />
24     <Button
25         android:text="呼叫"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:id="@+id/CallButton" />
29     <Button
30         android:text="@string/callHistory"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:id="@+id/CallHistoryButton"
34         android:enabled="false" />
35 </LinearLayout>

2.MainActivity.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using Android.App;
  4 using Android.Content;
  5 using Android.Runtime;
  6 using Android.Views;
  7 using Android.Widget;
  8 using Android.OS;
  9 
 10 namespace Phoneword_Droid
 11 {
 12     [Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
 13     public class MainActivity : Activity
 14     {
 15         int count = 1;
 16         static readonly List<string> phoneNumbers = new List<string>();
 17 
 18         protected override void OnCreate(Bundle bundle)
 19         {
 20             //button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
 21 
 22             base.OnCreate(bundle);
 23             SetContentView(Resource.Layout.Main);
 24 
 25             EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
 26             Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
 27             Button callButton = FindViewById<Button>(Resource.Id.CallButton);
 28             Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton);
 29 
 30             callButton.Enabled = false;
 31 
 32             #region//转化拨号
 33             string translatedNumber = string.Empty;
 34             translateButton.Click += (object sender, EventArgs e) =>
 35             {
 36                 translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text);
 37                 if (String.IsNullOrWhiteSpace(translatedNumber))
 38                 {
 39                     callButton.Text = "Call";
 40                     callButton.Enabled = false;
 41                 }
 42                 else
 43                 {
 44                     callButton.Text = "Call" + translatedNumber;
 45                     callButton.Enabled = true;
 46                 }
 47             };
 48             #endregion
 49 
 50             #region//拨打电话
 51             callButton.Click += (s, e) =>
 52             {
 53                 //对话框
 54                 var callDialog = new AlertDialog.Builder(this);
 55 
 56                 //对话框内容
 57                 callDialog.SetMessage("Call" + translatedNumber + "?");
 58 
 59                 //拨打按钮
 60                 callDialog.SetNeutralButton("Call", delegate
 61                 {
 62                     ////使用意图拨打电话
 63                     //var callIntent = new Intent(Intent.ActionCall);
 64 
 65                     ////将需要拨打的电话设置为意图的参数
 66                     //callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
 67 
 68                     //StartActivity(callIntent);
 69 
 70                     //将电话加入到历史记录列表中
 71                     phoneNumbers.Add(translatedNumber);
 72 
 73                     //如果callHistoryButton的定义在这段代码后面将会出错,所以我们这个时候需要将
 74                     //Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代码提前
 75                     callHistoryButton.Enabled = true;
 76 
 77                     //使用意图拨打电话
 78                     var callIntent = new Intent(Intent.ActionCall);
 79 
 80                     //将需要拨打的电话设置为意图的参数
 81                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
 82 
 83                     StartActivity(callIntent);
 84 
 85                 });
 86 
 87                 //取消按钮
 88                 callDialog.SetNegativeButton("Cancel", delegate { });
 89 
 90                 //显示对话框
 91                 callDialog.Show();
 92             };
 93             #endregion
 94 
 95             #region//通话记录
 96             callHistoryButton.Click += (e, t) =>
 97             {
 98                 //指定意图需要打开的活动
 99                 var intent = new Intent(this, typeof(CallHistoryActivity));
100                 //设置意图传递的参数
101                 intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
102                 StartActivity(intent);
103             };
104             #endregion
105 
106         }
107     }
108 }
View Code

3.PhoneTranslator.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using Android.App;
  4 using Android.Content;
  5 using Android.Runtime;
  6 using Android.Views;
  7 using Android.Widget;
  8 using Android.OS;
  9 
 10 namespace Phoneword_Droid
 11 {
 12     [Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
 13     public class MainActivity : Activity
 14     {
 15         int count = 1;
 16         static readonly List<string> phoneNumbers = new List<string>();
 17 
 18         protected override void OnCreate(Bundle bundle)
 19         {
 20             //button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
 21 
 22             base.OnCreate(bundle);
 23             SetContentView(Resource.Layout.Main);
 24 
 25             EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
 26             Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
 27             Button callButton = FindViewById<Button>(Resource.Id.CallButton);
 28             Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton);
 29 
 30             callButton.Enabled = false;
 31 
 32             #region//转化拨号
 33             string translatedNumber = string.Empty;
 34             translateButton.Click += (object sender, EventArgs e) =>
 35             {
 36                 translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text);
 37                 if (String.IsNullOrWhiteSpace(translatedNumber))
 38                 {
 39                     callButton.Text = "Call";
 40                     callButton.Enabled = false;
 41                 }
 42                 else
 43                 {
 44                     callButton.Text = "Call" + translatedNumber;
 45                     callButton.Enabled = true;
 46                 }
 47             };
 48             #endregion
 49 
 50             #region//拨打电话
 51             callButton.Click += (s, e) =>
 52             {
 53                 //对话框
 54                 var callDialog = new AlertDialog.Builder(this);
 55 
 56                 //对话框内容
 57                 callDialog.SetMessage("Call" + translatedNumber + "?");
 58 
 59                 //拨打按钮
 60                 callDialog.SetNeutralButton("Call", delegate
 61                 {
 62                     ////使用意图拨打电话
 63                     //var callIntent = new Intent(Intent.ActionCall);
 64 
 65                     ////将需要拨打的电话设置为意图的参数
 66                     //callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
 67 
 68                     //StartActivity(callIntent);
 69 
 70                     //将电话加入到历史记录列表中
 71                     phoneNumbers.Add(translatedNumber);
 72 
 73                     //如果callHistoryButton的定义在这段代码后面将会出错,所以我们这个时候需要将
 74                     //Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代码提前
 75                     callHistoryButton.Enabled = true;
 76 
 77                     //使用意图拨打电话
 78                     var callIntent = new Intent(Intent.ActionCall);
 79 
 80                     //将需要拨打的电话设置为意图的参数
 81                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
 82 
 83                     StartActivity(callIntent);
 84 
 85                 });
 86 
 87                 //取消按钮
 88                 callDialog.SetNegativeButton("Cancel", delegate { });
 89 
 90                 //显示对话框
 91                 callDialog.Show();
 92             };
 93             #endregion
 94 
 95             #region//通话记录
 96             callHistoryButton.Click += (e, t) =>
 97             {
 98                 //指定意图需要打开的活动
 99                 var intent = new Intent(this, typeof(CallHistoryActivity));
100                 //设置意图传递的参数
101                 intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
102                 StartActivity(intent);
103             };
104             #endregion
105 
106         }
107     }
108 }
View Code

4.CallHistoryActivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Phoneword_Droid
{
    [Activity(Label = "@string/callHistory")]
    public class CallHistoryActivity : ListActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //从意图中获取传递过来的参数
            var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];

            //将字符串数组显示到列表控件中(因为继承的是ListActivity所以整个视图就是一个列表)
            this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);

            //关于ArrayAdapter的第二个参数,其实就是指定列表中每个项的视图,后面我们会通过自定义的方式控制列表的项


        }
    }
}

模拟器

运行效果