Xamarin.Android之轉換,呼叫,查看歷史紀錄

                                                         Xamarin.Android之轉換,呼叫,查看歷史紀錄android

        E文文章。ide

       功能:能將輸入的字母轉換成相應的數字。而且能呼叫出去。能查看呼叫的歷史紀錄。ui

       界面代碼以下:   this

 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代碼。spa

 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代碼。code

 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 }
相關文章
相關標籤/搜索