原文 Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发
前言
大部份的Android 都具有实体或虚拟的Back键. 因此在处理多页面应用程式时, 与先前所介绍的iOS Navigation controller 比较起来会简单许多.
1. 开启Visual Studio 并新增Android Application 专案并命名为Lab4-MultiScreen
2. 在Layout资料夹中新增Second.axml
在Second.axml 中拖放1个TextView并标示此为第2个Activity
2. 在专案底下新增一个SecondActivity.cs. 在OnCreate事件中撰写以下程式码:
SetContentView(Resource.Layout.Second);
3. 开启Activity1.cs, 在class name的地方按滑鼠右键=>重构=>重新命名. 将类别名称改为FirstActivity. 记得在方案总管中的档名也一并改为FirstActivity.cs
4. 开启Main.axml, 在画面中放置1个Button并指定Text属性值为”Load Second Activity”并将id 的属性值变更为”@+id/ShowSecond”
5. 开启FirstActivity.cs, 在OnCreate事件中撰写以下程式码:
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.
- 方法一: 使用内建的StartActivity方法, 程式码如下:
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>
|
- 方法二: 建立Intent, 然后使用StartActivity载入其他SecondActivity. 程式码如下:
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>
|
- 方法三: 建立Intent, 并透过Intent.PutExtra载入Activity并传入参数. 程式码如下:
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
本文同时刊载于昕力资讯网站,转载请注明出处!