Xamarin.Android多界面


一、准備

開始學習本教程前必須先完成該教程http://www.cnblogs.com/yaozhenfa/p/xamarin_android_quickstart.html 否則將無法繼續。

 

二、界面

1.打開Resources/layout/Main.axml文件,並在Call Button下方繼續加入一個按鈕,並設置其id為@+id/CallHistoryButton同時設置Text為@string/callHistory(這個其實是一個字符串資源的標識符,后面我們會添加該資源):

 

三、資源

1.打開Resources/values/Strings.xml文件

 

2.並在其中加入一個name為callHistory的字符串資源:

 

3.回到Main.axml可以看到最后一個button顯示的字符串變掉了:

 

4.之前的Call button是通過代碼的方式禁用的,這次我們將CallHistory Button通過屬性該改變:

 

可以看到按鈕被禁用了:

 

四、代碼

1.右擊項目,新建一個名為CallHistoryActivity的活動:

 

2.打開剛才新建的活動,修改該活動的標題名稱,繼承的類並顯示傳遞過來的字符串數組:

 1 namespace Phoneword_Droid
 2 {
 3     [Activity(Label = "@string/callHistory")]
 4     public class CallHistoryActivity : ListActivity
 5     {
 6         protected override void OnCreate(Bundle bundle)
 7         {
 8             base.OnCreate(bundle);
 9             //從意圖中獲取傳遞過來的參數
10             var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];
11 
12             //將字符串數組顯示到列表控件中(因為繼承的是ListActivity所以整個視圖就是一個列表)
13             this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);
14 
15             //關於ArrayAdapter的第二個參數,其實就是指定列表中每個項的視圖,后面我們會通過自定義的方式控制列表的項
16         }
17     }
18 }
View Code

 

 

3.回到MainActivity.cs中,既然要顯示歷史記錄,那么自然就必須要能夠保存所以我們需要定義一個變量:

1     [Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
2     public class MainActivity : Activity
3     {
4         static readonly List<string> phoneNumbers = new List<string>();
View Code

 

 

4.然后還要為callHistoryButton綁定監聽事件,以便打開另一個活動(在OnCreate后面繼續追加):

1             Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton);
2             callHistoryButton.Click += (e, t) =>
3             {
4                 //指定意圖需要打開的活動
5                 var intent = new Intent(this, typeof(CallHistoryActivity));
6                 //設置意圖傳遞的參數
7                 intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
8                 StartActivity(intent);
9             };
View Code

 

 

5.我們缺少一個添加歷史記錄的方法,這里我們應該將其放入對話框的Call方法中,這樣只要撥打了的電話才會進入到歷史記錄中:

 1                 //撥打按鈕
 2                 callDialog.SetNeutralButton("Call", delegate
 3                 {
 4                     //將電話加入到歷史記錄列表中
 5                     phoneNumbers.Add(translatedNumber);
 6 
 7                     //如果callHistoryButton的定義在這段代碼后面將會出錯,所以我們這個時候需要將
 8                     //Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代碼提前
 9                     callHistoryButton.Enabled = true;
10 
11                     //使用意圖撥打電話
12                     var callIntent = new Intent(Intent.ActionCall);
13 
14                     //將需要撥打的電話設置為意圖的參數
15                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
16                     
17                     StartActivity(callIntent);
18                 });
View Code

 

 

五、運行

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM