原文:Xamarin.Android開發實踐(二)html
能夠看到按鈕被禁用了:android
namespace Phoneword_Droid { [Activity(Label = "@string/callHistory")] public class CallHistoryActivity : ListActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); //從意圖中獲取傳遞過來的參數 var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0]; //將字符串數組顯示到列表控件中(由於繼承的是ListActivity因此整個視圖就是一個列表) this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers); //關於ArrayAdapter的第二個參數,其實就是指定列表中每一個項的視圖,後面咱們會經過自定義的方式控制列表的項 } } }
[Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { static readonly List<string> phoneNumbers = new List<string>();
Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); callHistoryButton.Click += (e, t) => { //指定意圖須要打開的活動 var intent = new Intent(this, typeof(CallHistoryActivity)); //設置意圖傳遞的參數 intent.PutStringArrayListExtra("phone_numbers", phoneNumbers); StartActivity(intent); };
//撥打按鈕 callDialog.SetNeutralButton("Call", delegate { //將電話加入到歷史記錄列表中 phoneNumbers.Add(translatedNumber); //若是callHistoryButton的定義在這段代碼後面將會出錯,因此咱們這個時候須要將 //Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代碼提早 callHistoryButton.Enabled = true; //使用意圖撥打電話 var callIntent = new Intent(Intent.ActionCall); //將須要撥打的電話設置爲意圖的參數 callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); StartActivity(callIntent); });