Hello, Android Multiscreen Xamarin.Android 中處理導航 在這兩部指南中,我們將擴展我們前面創建的Phoneword 應用,以處理第二個屏幕。本章主要介紹安卓的一些基本模塊的構建,同時也會深入講解安卓的架構,以便我們更好的瞭解安卓的結構和功能。 在這兩部指南中 ...
Hello, Android Multiscreen
Xamarin.Android 中處理導航
在這兩部指南中,我們將擴展我們前面創建的Phoneword 應用,以處理第二個屏幕。本章主要介紹安卓的一些基本模塊的構建,同時也會深入講解安卓的架構,以便我們更好的瞭解安卓的結構和功能。
你好,安卓的多屏幕快速入門在本指南的演練部分我們將向我們的Phoneword應用程添加一個通話記錄的功能,即添加第二個屏幕。最終的應用程式將有第二個屏幕顯示通話記錄,如下圖所示 ︰
在隨附的深入小節中,我們將回顧我們已經構建的應用、 導航和其他我們前進道路上遇到的新的安卓概念。
讓我們開始吧 !
要求因為本指南是你好,Android的後續,在開始本章之前你應該先完成你好,Android 快速入門。如果你想要直接進入下麵的演練中,您可以下載已完成的版本的Phoneword (從你好,Android 快速入門) 並用於開始演練。
演練在本演練中我們將向我們的Phoneword應用程式添加通話記錄屏幕。
1. 讓我們在 Visual Studio 中打開Phoneword應用程式 ︰





7.在Strings.xml中設置callHistory的字元串名稱和值並保存。 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="callHistory">Call History</string> </resources> 通話記錄按鈕文本應更新以反映新的字元串值 ︰




using System; using System.Collections.Generic; using Android.App; using Android.OS; using Android.Widget; namespace Phoneword { [Activity(Label = "@string/callHistory")] public class CallHistoryActivity : ListActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Create your application here var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0]; this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers); } } }在這節課,我們要創建一個ListActivity對象並實現它的數據填充的代碼,所以我們不需要為這一活動創建一個新的佈局文件。我們會在Hello, Android Multiscreen Deep Dive. 一節討論這個問題. 12.在我們的應用程式,我們要獲取用戶在第一個屏幕上撥的電話號碼,然後將它們傳遞到第二個屏幕。我們要將電話號碼存儲為字元串集合。為了支持列表,請將以下using指令添加到MainActivity類的頂部 ︰
using System.Collections.Generic;
接下來,讓我們創建一個空的列表,用來存放電話號碼。我們的MainActivity類將如下所示 ︰
[Activity(Label = "Phoneword", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { static readonly List<string> phoneNumbers = new List<string>(); ...// OnCreate, etc. }讓我們處理Call History按鈕。在MainActivity類中,添加以下代碼以註冊並獲取按鈕 ︰
Button callHistoryButton = FindViewById<Button> (Resource.Id.CallHistoryButton); callHistoryButton.Click += (sender, e) => { var intent = new Intent(this, typeof(CallHistoryActivity)); intent.PutStringArrayListExtra("phone_numbers", phoneNumbers); StartActivity(intent); };14.我們想要擴展調用按鈕的功能,當用戶撥打新號碼時,將電話號碼添加到數字列表中並啟用通話記錄按鈕。讓我們修改彈框中Neutral 的 代碼︰
callDialog.SetNeutralButton("Call", delegate { // add dialed number to list of called numbers. phoneNumbers.Add(translatedNumber); // enable the Call History button callHistoryButton.Enabled = true; // Create intent to dial phone var callIntent = new Intent(Intent.ActionCall); callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); StartActivity(callIntent); });保存並生成應用程式,以確保沒有錯誤。 15. 部署應用程式到模擬器或設備。下麵的截圖是Phoneword應用程式在 Xamarin 安卓播放機中運行的 結果︰
