Xamarin.Android之转换,呼叫,查看历史纪录

                                                         Xamarin.Android之转换,呼叫,查看历史纪录

        E文文章。

       功能:能将输入的字母转换成相应的数字。并且能呼叫出去。能查看呼叫的历史纪录。

       界面代码如下:   

 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 
 7   <EditText
 8           android:layout_width="fill_parent"
 9      android:layout_height="wrap_content"
10        android:text="0712-XAMARIN"
11           android:id="@+id/et"
12     />
13 
14   <Button
15              android:layout_width="fill_parent"
16      android:layout_height="wrap_content"
17        android:text="转换"
18           android:id="@+id/btnTran"
19     />
20 
21   <Button
22            android:layout_width="fill_parent"
23        android:layout_height="wrap_content"
24        android:text="呼叫"
25           android:id="@+id/btnCall"
26     />
27 
28   <Button
29             android:layout_width="fill_parent"
30         android:layout_height="wrap_content"
31         android:text="历史纪录"
32            android:id="@+id/btnCallHistory"
33     />
34 
36 </LinearLayout>

 

     主Activity代码。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Android.App;
 7 using Android.Content;
 8 using Android.OS;
 9 using Android.Runtime;
10 using Android.Views;
11 using Android.Widget;
12 
13 namespace App3
14 {
15     [Activity(Label = "CallActivity", MainLauncher = true, Icon = "@drawable/icon2")]
16     public class CallActivity : Activity
17     {
18         //定义手机集合。
19         private static readonly List<string> PhoneNumbers = new List<string>();
20         protected override void OnCreate(Bundle bundle)
21         {
22             base.OnCreate(bundle);
23             SetContentView(Resource.Layout.Call);
24             EditText et = FindViewById<EditText>(Resource.Id.et);
25             Button btnTran = FindViewById<Button>(Resource.Id.btnTran);
26             Button btnCall = FindViewById<Button>(Resource.Id.btnCall);
27             btnCall.Enabled = false;
28             Button btnCallHistory = FindViewById<Button>(Resource.Id.btnCallHistory);
29             btnCallHistory.Enabled = false;
30             string translatedNumber = string.Empty;
31 
32 
33             btnTran.Click += (sender, e) =>
34             {
35                 translatedNumber = PhoneTranslator.ToNumber(et.Text);
36                 //将转换的手机号加入到手机集合中。
37                 PhoneNumbers.Add(translatedNumber);
38                 btnCallHistory.Enabled = true;
39                 if (String.IsNullOrWhiteSpace(translatedNumber))
40                 {
41                     btnCall.Text = "呼叫";
42                     btnCall.Enabled = false;
43                 }
44                 else
45                 {
46                     btnCall.Text = "呼叫" + translatedNumber;
47                     btnCall.Enabled = true;
48                 }
49             };
50 
51 
52             btnCall.Click += (sender, e) =>
53             {
54                 //对话框
55                 var callDialog = new AlertDialog.Builder(this);
56                 callDialog.SetMessage("呼叫" + translatedNumber + "?");
57                 //拨打按钮
58                 callDialog.SetNeutralButton("呼叫", delegate
59                 {
60                     //使用意图拨打电话
61                     var callIntent = new Intent(Intent.ActionCall);
62                     //将需要拨打的电话设置为意图的参数.注意写法
63                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
64                     StartActivity(callIntent);
65                 });
66                 callDialog.SetNegativeButton("取消", delegate { });
67                 callDialog.Show();
68             };
69 
70 
71             btnCallHistory.Click += (sender, e) =>
72             {
73                 //用意图打开历史纪录的活动
74                 Android.Content.Intent it = new Intent(this, typeof(CallHistoryActiviry));
75                 it.PutStringArrayListExtra("phoneNumbers", PhoneNumbers);
76                 StartActivity(it);
77             };
78 
79         }
80     }
81 }

 

 

    通话纪录的Activity代码。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Android.App;
 7 using Android.Content;
 8 using Android.OS;
 9 using Android.Runtime;
10 using Android.Views;
11 using Android.Widget;
12 
13 namespace App3
14 {
15     [Activity(Label = "CallHistoryActiviry")]
16     public class CallHistoryActiviry : ListActivity
17     {
18         protected override void OnCreate(Bundle bundle)
19         {
20             base.OnCreate(bundle);
21             var phoneNumbers = Intent.Extras.GetStringArrayList("phoneNumbers") ?? new string[0];
22             //只有当此Activity继承于ListActivity时,整个视图才是列表,才可以这么写。
23             this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1, phoneNumbers);
24         }
25     }
26 }

 

 

       

posted @ 2015-05-05 15:02  公子若不胖天下谁胖  阅读(402)  评论(0编辑  收藏  举报