android多屏应用程序,了解 Android 多屏显示:快速入门

首先在 Visual Studio 中打开 Phoneword 应用程序,然后从“解决方案资源管理器”中编辑 Main.axml 文件 。Start by opening the Phoneword application in Visual Studio and editing the Main.axml file from the Solution Explorer.

提示

Visual Studio 的较新版本支持在 Android Designer 中打开 .xml 文件。Newer releases of Visual Studio support opening .xml files inside the Android Designer.

.axml 和 .xml 文件均受 Android Designer 支持。Both .axml and .xml files are supported in the Android Designer.

更新布局Updating the layout

从“工具箱”中将“按钮”拖动到 Design Surface 上,然后将其置于“TranslatedPhoneWord”TextView 下方。From the Toolbox, drag a Button onto the design surface and place it below the TranslatedPhoneWord TextView. 在“属性面板”窗格中,将按钮“ID”更改为 @+id/TranslationHistoryButtonIn the Properties pane, change the button Id to @+id/TranslationHistoryButton

60b96e4a71ed91904fb51e6a50014a1a.png60b96e4a71ed91904fb51e6a50014a1a.png

将按钮的 Text 属性设为 @string/translationHistory。Set the Text property of the button to @string/translationHistory. Android 设计器将按字面意思解读此属性,但用户需要做些更改,使按钮的文本正确显示:The Android Designer will interpret this literally, but you're going to make a few changes so that the button's text shows up correctly:

528c5ba93373a57092a5605d12d7b0d9.png528c5ba93373a57092a5605d12d7b0d9.png

在解决方案资源管理器的“资源” 文件夹下展开“值” 节点,然后双击字符串资源文件 Strings.xml:Expand the values node under the Resources folder in the Solution Explorer and double-click the string resources file, Strings.xml:

6417dd08c88ebf55b81a3c5abc0b6308.png6417dd08c88ebf55b81a3c5abc0b6308.png

向 Strings.xml 文件添加 translationHistory 字符串名称和值,然后保存该文件:Add the translationHistory string name and value to the Strings.xml file and save it:

Translation History

Phoneword

“转换历史记录”按钮文本应会更新以反映新的字符串值 :The Translation History button text should update to reflect the new string value:

3f1376facfe7258ee8fa63fd2bc15b5f.png3f1376facfe7258ee8fa63fd2bc15b5f.png

在 Design Surface 上选中“转换历史记录”按钮后,在“属性面板”窗格中查找 enabled 设置,然后将其值设为 false 以禁用此按钮 。With the Translation History button selected on the design surface, find the enabled setting in the Properties pane and set its value to false to disable the button. 这将导致按钮在设计图面上颜色变暗:This will cause the button to become darker on the design surface:

15b69f796afa876873aa07af71cf4110.png15b69f796afa876873aa07af71cf4110.png

创建第二个活动Creating the second activity

再创建一个“活动”以支持第二个屏幕。Create a second Activity to power the second screen. 在“解决方案资源管理器” 中,右键单击“Phoneword” 项目,然后选择“添加”>“新建项…” :In the Solution Explorer, right-click the Phoneword project and choose Add > New Item...:

e68a78afd8003bcca92345e9723220fd.pnge68a78afd8003bcca92345e9723220fd.png

在“添加新项”对话框中,选择“Visual C#”>“活动”,然后将活动文件命名为 TranslationHistoryActivity.cs 。In the Add New Item dialog, choose Visual C# > Activity and name the Activity file TranslationHistoryActivity.cs.

将 TranslationHistoryActivity.cs 中的模板代码替换为以下代码 :Replace the template code in TranslationHistoryActivity.cs with the following:

using System;

using System.Collections.Generic;

using Android.App;

using Android.OS;

using Android.Widget;

namespace Phoneword

{

[Activity(Label = "@string/translationHistory")]

public class TranslationHistoryActivity : 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(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);

}

}

}

在此类中,将按编程方式创建和填充 ListActivity,因此无需新建此活动的布局文件。In this class, you're creating a ListActivity and populating it programmatically, so you don't need to create a new layout file for this Activity. This is discussed in more detail in the Hello, Android Multiscreen Deep Dive.

添加列表Adding a list

此应用会收集电话号码(用户已在第一个屏幕上转换的),然后传递给第二个屏幕。This app collects phone numbers (that the user has translated on the first screen) and passes them to the second screen. 电话号码以字符串列表的形式存储。The phone numbers are stored as a list of strings. 若要支持列表(和稍后使用的“意向”),请将以下 using 指令添加到 MainActivity.cs 顶部 :To support lists (and Intents, which are used later), add the following using directives to the top of MainActivity.cs:

using System.Collections.Generic;

using Android.Content;

然后请创建可使用电话号码填充的空白列表。Next, create an empty list that can be filled with phone numbers.

MainActivity 类将如下所示:The MainActivity class will look like this:

[Activity(Label = "Phoneword", MainLauncher = true)]

public class MainActivity : Activity

{

static readonly List phoneNumbers = new List();

...// OnCreate, etc.

}

在 MainActivity 类中,添加以下代码以注册“转换历史记录”按钮(将此行放在 translateButton 声明后) :In the MainActivity class, add the following code to register the Translation History button (place this line after the translateButton declaration):

Button translationHistoryButton = FindViewById (Resource.Id.TranslationHistoryButton);

将以下代码添加到 OnCreate 方法的末尾,以关联“转换历史记录” 按钮:Add the following code to the end of the OnCreate method to wire up the Translation History button:

translationHistoryButton.Click += (sender, e) =>

{

var intent = new Intent(this, typeof(TranslationHistoryActivity));

intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);

StartActivity(intent);

};

更新“转换”按钮将电话号码添加到 phoneNumbers 列表 。Update the Translate button to add the phone number to the list of phoneNumbers. 用于 translateButton 的 Click 处理程序应与以下代码类似:The Click handler for the translateButton should resemble the following code:

// Add code to translate number

string translatedNumber = string.Empty;

translateButton.Click += (sender, e) =>

{

// Translate user's alphanumeric phone number to numeric

translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);

if (string.IsNullOrWhiteSpace(translatedNumber))

{

translatedPhoneWord.Text = "";

}

else

{

translatedPhoneWord.Text = translatedNumber;

phoneNumbers.Add(translatedNumber);

translationHistoryButton.Enabled = true;

}

};

保存并生成应用程序,确保没有错误。Save and build the application to make sure there are no errors.

运行应用程序Running the app

向仿真器或设备部署应用程序。Deploy the application to an emulator or device. 下面的屏幕截图描述了正在运行的 Phoneword 应用程序:The following screenshots illustrate the running Phoneword application:

6d329d87e28cbe88e43a9cae98de13fa.png6d329d87e28cbe88e43a9cae98de13fa.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值