[译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手
[001]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/
[002]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/
[003]: http://www.shisujie.com/blog/Hello-Android-Quickstart
[004]: https://developer.xamarin.com/samples/monodroid/Phoneword/
[005]: http://git.oschina.net/huaxia283611/XamarinDoc/tree/master/Samples/Phoneword
[006]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_deepdive/
[101]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/intro-screenshot.png
[102]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/01-phoneword.png
[103]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/02-main-axml.png
[104]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/03-new-button.png
[105]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/04-call-history-button.png
[106]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/05-call-history-string.png
[107]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/06-strings-resources-file.png
[108]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/07-new-string-value.png
[109]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/08-enabled-false.png
[110]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/09-add-new-file.png
[111]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/10-call-history-activity.png
[112]: https://developer.xamarin.com/guides/android/getting_started/hello ,android_multiscreen/hello,android_multiscreen_quickstart/Images/vs/phoneword-xap.png
原文链接:[Hello, Android Multiscreen Quickstart][001].
译文链接:Hello,Android Multiscreen快速上手
本部分介绍利用Xamarin.Android处理导航
Hello, Android Multiscreen Quickstart
在指南的第一部分,我们将为Phoneword添加第二个屏幕 —— 关于电话历史的跟踪情况。最终的应用会有第二个屏幕来展示拨打历史。最终界面如下:
![][101]
下面开始练习。
系统及环境要求
由于本教程将在Hello,Android Quickstart基础上进行操作,你需要完成Hello,Android Quickstart部分内容。如果你想跳过上述步骤,直接进行本文内容,你可以下载现有的Phoneword代码进行演练,相关链接地址如下:
- Hello,Android Quickstart指南地址:[原文][002],[译文][003];
- Hello,Android Quickstart示例下载地址:[英文原始示例][004],[译者自建使用示例][005]。
操作演练
在演练中,我们会为 Phoneword 应用添加Call History界面。
1.在Visual Studio中打开 Phoneword 应用项目
![][102]
2.编辑交互界面。在解决方案中打开Main.axml文件:
![][103]
3.在 工具箱 中拖动一个 按钮-Button 到设计界面,并将其放置在 拨打-Call 按钮下方:
![][104]
4.在 属性 面板中,修改按钮的 Id 属性值为 @+id/CallHistoryButton:
![][105]
5.然后将按钮的 Text 属性值设为 @string/callHistory。Android设计器会按照字面上的解释显示。不过我们会进行一些修改来让按钮的文本正确显示:
![][106]
6.在解决方案管理器中,展开Resources文件夹下的value节点,然后双击字符串资源文件——Strings.xml:
![][107]
7.在Strings.xml文件中添加名为callHistory的字符串,并为其设值,然后保存,代码如下:
这样Android设计器中,Call History 按钮的文本会更新显示为新设的字符串:
![][108]
8.在界面选中 Call History 按钮,然后再 属性 面板中找到enabled设置,并将其设置为false将按钮禁用。此操作会导致按钮在设计界面中显示为暗色:
![][109]
9.现在,为应用添加第二个Activity来为第二个界面提供服务。首先,在解决方案管理器中,右击Phoneword项目,选择 添加-新建项 :
![][110]
10.在 新建项 对话框中,选择 Visual C#-Activity,并将Activity文件名设为CallHistoryActivity.cs:
![][111]
11.用以下的代码替换CallHistoryActivity.cs中的模板代码:
using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Widget;
namespace Phoneword
{
[Activity(Label = "@string/callHistory")]
public class CallHistoryActivity : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];
this.ListAdapter = new ArrayAdapter
}
}
}
在此类中,我们创建了ListActivity,然后用代码编辑它,故此处我们无需为其创建新的布局文件。我们会在[原文:Hello, Android Multiscreen Deep Dive][006]中详细讨论。—— 官方文档此处链接指向错误,此处调整
12.在我们的应用中,我们将收集用户在第一个屏幕中拨打的电话号码,然后将其传递到第二个屏幕。其中,我们将电话号码通过字符串列表存储。要使用列表,我们需要在MainActivity类上面添加如下引用:
using System.Collections.Generic;
接下来,我们创建空列表——用于存储电话号码。MainActivity类代码将类似如下代码:
[Activity(Label = "Phoneword", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
static readonly List
...// OnCreate, etc.
}
13.下面,我们将 Call History 按钮与代码连接上。在MainActivity类中,添加如下注册和链接代码:
Button callHistoryButton = FindViewById
个人网站:http://www.shisujie.com

浙公网安备 33010602011771号