首先在Visual Studio中打开phone word APP应用程序,然后从解决方案资源管理器中编辑Main.axml文件。 startbyopeningthephonewordapplicationinvisualstudioandeditingthemain.axmlfilefromthesolutionexplorer。
提示
新版本的Visual Studio支持在Android Designer中打开. xml文件。 newerreleasesofvisualstudiosupportopening.xmlfilesinsidetheandroiddesigner。
Android Designer支持所有. axml和. xml文件。 both.ax mland.xmlfilesaresupportedintheandroiddesigner。
更新布局更新布局
从工具箱中将按钮拖动到Design Surface中,并将其放置在TranslatedPhoneWord”TextView下。 从工具箱, 在dragabuttonontothedesignsurfaceandplaceitbelowthetranslatedphonewordtextview .属性面板的窗格中,将按钮“ID”改为@ id/translationd 更改为changethebuttonidto @ id/translationhistorybutton
将按钮的Text属性设置为@string/translationHistory。 setthetextpropertyofthebuttonto @ string/translation history.Android设计器按字面解释此属性,但用户必须进行更改。 正确显示按钮文本: theandroiddesignerwillinterpretthisliterally,but you ' regoingtomakeafewchangessothatthebuton ' stextshowshowstton
展开解决方案资源管理器的“资源”文件夹下的“值”节点,然后展开字符串资源文件strings.XML:expandthevaluesnodeundertheresourcesfolderinthesoll
将translationHistory字符串名称和值添加到Strings.xml文件中,并将其添加到addthetranslationhistorystringnameandvaluetothestrings.xmlfileandsaveit 3360文件中
翻译历史
Phoneword
“转换历史记录”按钮中的文本必须更新,以反映名为thetranslationhistorybuttontextshouldupdatetoreflectthenewstringvalue 3360的新字符串值
在Design Surface中选择“转换历史记录”按钮后,在“属性面板”窗格中找到启用设置,然后将值设置为false以禁用此按钮。 withetranslationhistorybuttonselectedonthedesignsurface,findtheenabledsettinginthepropertiespaneandsetitsvaluetsvaluetofalsetodise thiswillcausethebuttontobecome
创建第二个活动的Creating the second activity
创建另一个支持第二个屏幕的“活动”。 在createasecondactivitytopowerthesecondscreen .解决方案资源管理器中,右键单击Phoneword项目,然后单击“添加”“新建项目…”: inthesolutionexexex
dd > New Item...:在“添加新项”对话框中,选择“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: