Hello, Android Multiscreen



Xamarin.Android 中处理导航



在这两部指南中,我们将扩展我们前面创建的Phoneword 应用,以处理第二个屏幕。本章主要介绍安卓的一些基本模块的构建,同时也会深入讲解安卓的架构,以便我们更好的了解安卓的结构和功能。



你好,安卓的多屏幕快速入门



在本指南的演练部分我们将向我们的Phoneword应用程添加一个通话记录的功能,即添加第二个屏幕。最终的应用程序将有第二个屏幕显示通话记录,如下图所示 ︰

android多屏设备下toast显示 安卓多屏显示_应用程序

在随附的深入小节中,我们将回顾我们已经构建的应用、 导航和其他我们前进道路上遇到的新的安卓概念。

让我们开始吧 !



要求

 



因为本指南是你好,Android的后续,在开始本章之前你应该先完成你好,Android 快速入门。如果你想要直接进入下面的演练中,您可以下载已完成的版本的Phoneword(从你好,Android 快速入门) 并用于开始演练。


演练

 



在本演练中我们将向我们的Phoneword应用程序添加通话记录屏幕。



    1. 让我们在 Visual Studio 中打开Phoneword应用程序 ︰            



                                                                                                                                                                                                                                

android多屏设备下toast显示 安卓多屏显示_c#_02


让我们通过编辑用户界面开始。从解决方案资源管理器中打开Main.axml文件:             


 

android多屏设备下toast显示 安卓多屏显示_移动开发_03


    3. 从工具箱,将按钮拖到设计图面上并将它置于Call按钮下面 ︰      


 

android多屏设备下toast显示 安卓多屏显示_应用程序_04


在属性窗格中,更改按钮Id为@+id/CallHistoryButton:          


      

android多屏设备下toast显示 安卓多屏显示_Android_05


让我们来设置按钮的Text属性为@string/callHistory。Android设计界面将会原样显示我们输入的字符串,但我们要进行一些更改,以便该按钮的文本正常显示 ︰


android多屏设备下toast显示 安卓多屏显示_c#_06


展开解决方案资源管理器中的Resources 文件夹下 的values文件夹 ,双击字符串资源文件, Strings.xml:


android多屏设备下toast显示 安卓多屏显示_android多屏设备下toast显示_07


                

7.在Strings.xml中设置callHistory的字符串名称和值并保存。


<?xml version="1.0" encoding="utf-8"?>


<resources>


<string name="callHistory">Call History</string>


</resources>


通话记录按钮文本应更新以反映新的字符串值 ︰


android多屏设备下toast显示 安卓多屏显示_c#_08


8.在设计图面上选择通话记录按钮,在属性窗格中找到enabled设置和将其值设置为false来禁用该按钮。设计图面上的按钮将会变成灰色 ︰


android多屏设备下toast显示 安卓多屏显示_应用程序_09


让我们创建另一个活动来驱动第二个屏幕。在解决方案资源管理器中,右击Phoneword项目,选择添加 > 新项目......:


    

android多屏设备下toast显示 安卓多屏显示_c#_10


在添加新项对话框中,选择Visual C# > Activity修改文件名为CallHistoryActivity.cs:


    

android多屏设备下toast显示 安卓多屏显示_android多屏设备下toast显示_11


   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<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);
        }
    }
}

在这节课,我们要创建一个ListActivity对象并实现它的数据填充的代码,所以我们不需要为这一活动创建一个新的布局文件。我们会在Hello, Android Multiscreen Deep Dive. 一节讨论这个问题.

 


在我们的应用程序,我们要获取用户在第一个屏幕上拨的电话号码,然后将它们传递到第二个屏幕。我们要将电话号码存储为字符串集合。为了支持列表,请将以下using指令添加到MainActivity类的顶部 ︰


using System.Collections.Generic;


接下来,让我们创建一个空的列表,用来存放电话号码。我们的MainActivity类将如下所示 ︰


[Activity(Label = "Phoneword", MainLauncher = true, Icon = "@drawable/icon")]
     public class MainActivity : Activity
     {
     static readonly List<string> phoneNumbers = new List<string>();
      ...// OnCreate, etc.
     }


让我们处理Call History按钮。在MainActivity类中,添加以下代码以注册并获取按钮 ︰


Button callHistoryButton = FindViewById<Button> (Resource.Id.CallHistoryButton);
      callHistoryButton.Click += (sender, e) =>
     {
     var intent = new Intent(this, typeof(CallHistoryActivity));
     intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
     StartActivity(intent);
     };


我们想要扩展调用按钮的功能,当用户拨打新号码时,将电话号码添加到数字列表中并启用通话记录按钮。让我们修改弹框中Neutral 的 代码


callDialog.SetNeutralButton("Call", delegate
      {
     // add dialed number to list of called numbers.
      phoneNumbers.Add(translatedNumber);
     // enable the Call History button
     callHistoryButton.Enabled = true;
     // Create intent to dial phone
      var callIntent = new Intent(Intent.ActionCall);
     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
     StartActivity(callIntent);
      });


    保存并生成应用程序,以确保没有错误。


部署应用程序到模拟器或设备。下面的截图是Phoneword应用程序在 Xamarin 安卓播放机中运行的 结果︰


    

android多屏设备下toast显示 安卓多屏显示_c#_12


祝贺您第一次完成多屏幕 Xamarin.Android 应用程序 !现在到了剖析技能的时候了,我们将在下节进行讲解。