Xamarin.Android多介面

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

一、準備開始學習本教程前必須先完成該教程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 : Activity3     {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

 

 

五、運行

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.