Xamarin.Android 中處理導航
在這兩部指南中,我們將擴展我們前面創建的Phoneword 應用,以處理第二個屏幕。本章主要介紹安卓的一些基本模塊的構建,同時也會深入講解安卓的架構,以便我們更好的瞭解安卓的結構和功能。
在本指南的演練部分我們將向我們的Phoneword應用程添加一個通話記錄的功能,即添加第二個屏幕。最終的應用程序將有第二個屏幕顯示通話記錄,如下圖所示 ︰
在隨附的深入小節中,我們將回顧我們已經構建的應用、 導航和其他我們前進道路上遇到的新的安卓概念。
讓我們開始吧 !
因爲本指南是你好,Android的後續,在開始本章之前你應該先完成你好,Android 快速入門。如果你想要直接進入下面的演練中,您可以下載已完成的版本的Phoneword (從你好,Android 快速入門) 並用於開始演練。
在本演練中我們將向我們的Phoneword應用程序添加通話記錄屏幕。
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); } } }
using System.Collections.Generic;
[Activity(Label = "Phoneword", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { static readonly List<string> phoneNumbers = new List<string>(); ...// OnCreate, etc. }
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); };
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); });