原文 Visual Studio跨平臺開發實戰(5) - Xamarin Android多頁面應用程式開發android
前言ide
大部份的Android 都具備實體或虛擬的Back鍵. 所以在處理多頁面應用程式時, 與先前所介紹的iOS Navigation controller 比較起來會簡單許多.post
1. 開啓Visual Studio 並新增Android Application 專案並命名爲Lab4-MultiScreen網站
2. 在Layout資料夾中新增Second.axmlui
在Second.axml 中拖放1個TextView並標示此爲第2個Activitythis
2. 在專案底下新增一個SecondActivity.cs. 在OnCreate事件中撰寫如下程式碼:google
SetContentView(Resource.Layout.Second);
3. 開啓Activity1.cs, 在class name的地方按滑鼠右鍵=>重構=>從新命名. 將類別名稱改成FirstActivity. 記得在方案總管中的檔名也一併改成FirstActivity.csspa
4. 開啓Main.axml, 在畫面中放置1個Button並指定Text屬性值爲」Load Second Activity」並將id 的屬性值變動爲」@+id/ShowSecond」code
5. 開啓FirstActivity.cs, 在OnCreate事件中撰寫如下程式碼:xml
SetContentView(Resource.Layout.Main);
3 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //宣告並取得按鈕物件, 並在按鈕的click事件處理中載入SecondActivity</span> //宣告並取得按鈕物件, 並在按鈕的click事件處理中載入SecondActivity</span> |
4 |
5 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Button button = FindViewById< Button >(Resource.Id.showSecond);</span> Button button = FindViewById< Button >(Resource.Id.showSecond);</span> |
6 |
7 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >button.Click += delegate </span> button.Click += delegate </span> |
8 |
9 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{…….按鈕處理函式}</span> {…….按鈕處理函式}</span> |
Button的click處理函式中, 咱們將使用3種方法來載入SecondActivity.
1 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //呼叫其餘Activity的第一種方法(隱含的創建Intent)</span> //呼叫其餘Activity的第一種方法(隱含的創建Intent)</span> |
2 |
3 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >StartActivity( typeof (SecondActivity));</span> StartActivity( typeof (SecondActivity));</span> |
1 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //呼叫其餘Activity的第二種方法, 創建Intent, 然後使用StartActivity載入其餘Activity</span> //呼叫其餘Activity的第二種方法, 創建Intent, 而後使用StartActivity載入其餘Activity</span> |
2 |
3 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >var second = new Intent( this , typeof (SecondActivity));</span> var second = new Intent( this , typeof (SecondActivity));</span> |
4 |
5 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >StartActivity(second);</span> StartActivity(second);</span> |
1 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //使用Intent.PutExtra載入Activity並傳入參數var second = new Intent(this, typeof(SecondActivity));</span> //使用Intent.PutExtra載入Activity並傳入參數var second = new Intent(this, typeof(SecondActivity));</span> |
2 |
3 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >second.PutExtra( "FirstData" , "Data from FirstActivity" );</span> second.PutExtra( "FirstData" , "Data from FirstActivity" );</span> |
4 |
5 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >StartActivity(second);</span> StartActivity(second);</span> |
上述的3種方式, 第1個跟第2個是同樣的, 使用第1種方式, 會隱含創建一個Intent物件。
6. 執行專案並檢視結果.
7. 透過上述的第3個方法, 能夠像QueryString般傳遞參數到下一個Activity. 如今咱們開啓SecondActivity.cs. 透過Intent的GetStringExtra方法來取得參數的值. 在Oncreate方法中撰寫如下程式碼:
1 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //載入頁面SetContentView(Resource.Layout.Second);</span> //載入頁面SetContentView(Resource.Layout.Second);</span> |
2 |
3 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //宣告並取得TextView物件var label = FindViewById</span> //宣告並取得TextView物件var label = FindViewById</span> <textview> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" >(Resource.Id.screen2Label);</span> (Resource.Id.screen2Label);</span> |
4 |
5 |
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //透過Intent.GetStringExtra取得從前一個Activity所傳來的訊息label.Text = Intent.GetStringExtra("FirstData") ?? "Data not available";</span> //透過Intent.GetStringExtra取得從前一個Activity所傳來的訊息label.Text = Intent.GetStringExtra("FirstData") ?? "Data not available";</span> </textview> |
在上述程式碼中, 咱們透過Intent的GetStringExtra(「參數名稱」)來取得字串型別的參數. 事實上還能夠透過相似的方法取得不一樣型別的參數值. 以下圖所示:
而??陳述式則是用來判斷是否爲Null的方便寫法. 若取出的值爲Null則顯示後面的字串.
8. 執行專案並檢視結果, 以下圖所示
結語
在本篇文章中, 咱們介紹Android 應用程式在多頁面中的切換, 相較於iOS, Android 對於多頁面的處理較爲方便. 另外在Android中也提供Tab控制項在多頁面之間進行切換. 有興趣的朋友能夠參考如下文章:
Tab Layout
http://docs.xamarin.com/guides/android/user_interface/tab_layout
範例程式碼下載: Lab04-MultiScreen.zip
本文同時刊載於昕力資訊網站,轉載請註明出處!