原文 Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发

前言

大部份的Andr​​oid 都具有实体或虚拟的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事件中撰写以下程式码:

1 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">SetContentView(Resource.Layout.Second);</span> SetContentView(Resource.Layout.Second);</span>

3. 开启Activity1.cs, 在class name的地方按滑鼠右键=>重构=>重新命名. 将类别名称改为FirstActivity. 记得在方案总管中的档名也一并改为FirstActivity.cs

4. 开启Main.axml, 在画面中放置1个Button并指定Text属性值为”Load Second Activity”并将id 的属性值变更为”@+id/ShowSecond”

5. 开启FirstActivity.cs, 在OnCreate事件中撰写以下程式码:

1 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">//載入頁面SetContentView(Resource.Layout.Main);</span> //载入页面SetContentView(Resource.Layout.Main);</span>
2
3 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">//宣告並取得按鈕物件, 並在按鈕的click事件處理中載入SecondActivity</span> //宣告并取得按钮物件, 并在按钮的click事件处理中载入SecondActivity</span>
4
5 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">button.Click +=delegate</span> button.Click +=delegate</span>
8
9 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">{…….按鈕處理函式}</span> {…….按钮处理函式}</span>

Button的click处理函式中, 我们将使用3种方法来载入SecondActivity.

  • 方法一: 使用内建的StartActivity方法, 程式码如下:
1 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">//呼叫其他Activity的第一種方法(隱含的建立Intent)</span> //呼叫其他Activity的第一种方法(隐含的建立Intent)</span>
2
3 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">StartActivity(typeof(SecondActivity));</span> StartActivity(typeof(SecondActivity));</span>
  • 方法二: 建立Intent, 然后使用StartActivity载入其他SecondActivity. 程式码如下:
1 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">//呼叫其他Activity的第二種方法, 建立Intent, 然後使用StartActivity載入其他Activity</span> //呼叫其他Activity的第二种方法, 建立Intent, 然后使用StartActivity载入其他Activity</span>
2
3 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">var second =newIntent(this,typeof(SecondActivity));</span> var second =newIntent(this,typeof(SecondActivity));</span>
4
5 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">StartActivity(second);</span> StartActivity(second);</span>
  • 方法三: 建立Intent, 并透过Intent.PutExtra载入Activity并传​​入参数. 程式码如下:
1 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left">//載入頁面SetContentView(Resource.Layout.Second);</span> //载入页面SetContentView(Resource.Layout.Second);</span>
2
3 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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 <spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="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

本文同时刊载于昕力资讯网站,转载请注明出处!

更多相关文章

  1. android中异步任务AsyncTask的应用和工作原理
  2. android异步操作
  3. 史上最全的Android面试题集锦
  4. Android单元测试之Local unit tests(上)
  5. android代码混淆
  6. android 登陆、提交数据或加载数据时提示页面
  7. Android(安卓)的 Activity 组件详解
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)-- android activity 各种
  2. Android(安卓)WebView
  3. 我的Android进阶之旅------>Ubuntu下不能
  4. Android之getSystemService
  5. Android中通过getSystemService取得服务
  6. Android开发学习之设置Android壁纸的功能
  7. android中遇到问题总结
  8. android中的Handler(2)
  9. Android(安卓)音频管理器AudioManager类
  10. Android如何保存和读取设置