[译]:Xamarin.Android开发入门——Hello,Android快速上手
[001]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/ "译文012"
[002]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/Resources/XamarinAppIconsAndLaunchImages.zip
[003]: https://developer.xamarin.com/samples/monodroid/Phoneword
[004]: https://developer.xamarin.com/guides/android/getting_started/installation/
[005]: https://developer.xamarin.com/guides/android/getting_started/installation/accelerating_android_emulators
[006]: https://msdn.microsoft.com/en-us/library/mt228280.aspx
[007]: https://msdn.microsoft.com/zh-cn/library/mt228280.aspx
[008]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_deepdive/ "译文011"
[009]: https://developer.xamarin.com/guides/android/deployment ,_testing,_and_metrics/debug-on-emulator/android-sdk-emulator/
[010]: https://developer.xamarin.com/guides/android/application_fundamentals/understanding_android_api_levels/
[011]: http://www.shisujie.com/blog/Hello-Android-DeepDive "原文008"
[012]: http://www.shisujie.com/blog/Hello-Android-Quickstart "原文001"
[101]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/intro-app-examples-sml.png
[102]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/01-launch-sml.png
[103]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/02-new-project.png
[104]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/03-new-project-name-sml.png
[105]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/04-open-layout-sml.png
[106]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/05-large-text-sml.png
[107]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/06-enter-a-phoneword-sml.png
[108]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/07-plain-text-sml.png
[109]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/08-add-properties-sml.png
[110]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/09-drag-button-sml.png
[111]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/10-translate-button-sml.png
[112]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/11-drag-call-button-sml.png
[113]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/12-call-properties-sml.png
[114]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/13-add-new-item-sml.png
[115]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/14-add-class-sml.png
[116]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/16-mainactivity-sml.png
[117]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/21a-android-manifest-sml.png
[118]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/22-rebuild-all-succeeded.png
[119]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/24a-delete-icon-sml.png
[120]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/24b-icon-deleted-permanently.png
[121]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/25-add-existing-item-sml.png
[122]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/26-select-icon-sml.png
[123]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/27a-source-icons-sml.png
[124]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/27b-drag-icon-folders-sml.png
[125]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/28-specify-icon-sml.png
[126]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/29c-set-api-level-sml.png
[127]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/29d-select-virtual-device-sml.png
[128]: https://developer.xamarin.com/guides/android/getting_started/hello ,android/hello,android_quickstart/Images/vs/30-phoneword-result-sml.png
返回索引目录
原文链接:[Hello, Android_Quickstart][001].
译文链接:[Xamarin.Android开发入门——Hello,Android快速上手][012]
本部分介绍利用Xamarin开发Android应用程序
Hello, Android Quickstart
在指南的第一部分,我们创建一个打电话的应用,基础功能为:将输入含有字母和数字的电话号码转化为纯数字号码,然后拨打此号码。最终界面如下:
![][101]
下面开始练习。
系统及环境要求
Xamarin.Android需要以下任一环境
- 系统:OS X Yosemite及以上;最新版Xamarin Studio
- 系统:Windows 7及以上;最新版Xamarin Studio
- 系统:Windows 7及以上;Visual Studio专业版或更高版本
本教程假定您已安装最新版Xamarin.Android。Xamarin.Android安装教程:[原文:Xamarin.Android Installation][004]。开始前,请先下载并解压开发所需的资源文件:[Xamarin App Icons & Launch Screens下载][002]
模拟器配置
如果你使用Google的Android SDK模拟器,建议配置硬件加速。关于硬件加速配置的内容见:[原文:Accelerating Android Emulators with HAXM][005]
如果你使用Visual Studio的Android模拟器,则Hyper-V必须启用。关于Visual Studio Android 模拟器配置见:[英:System Requirements for the Visual Studio Emulator for Android.][006]、[中:Visual Studio Emulator for Android 系统要求][007]
操作演练
1.启动Visual Studio
![][102]
2.点击开始界面里的 开始-新建项目 来创建项目
![][103]
3.在 新建项目 对话框中,点击 C#-Android ,然后选择Blank App(Android) 模板。将项目命名为 Phoneword 。点击 确定 创建新项目
![][104]
4.项目创建成功后,在解决方案管理器中展开 Resources 文件夹里的 layout 文件夹。双击Main.axml 文件打开Android界面设计器,如下图所示:
![][105]
5.选中设计界面中的Hello World,Click Me!按钮,并按Delete键删除它。
在工具箱(左侧区域)的搜索框中输入text,然后拖动控件 Text(Large) 到设计界面(中间区域)中:
注:新版模板里已没有按钮,可略过删除
![][106]
6.选中 Text(Large) 控件,然后在属性面板中修改text属性值为Enter a Phoneword,如图所示:
![][107]
7.下一步,在工具箱中拖动一个 Plain Text 控件到设计界面中,将它放置在 Text(Large) 控件下面。
注:可以通过搜索框帮助我们快速定位到具体的控件。
![][108]
8.选中 Plain Text 控件,在属性面板中修改id属性值为@+id/PhoneNumberText,同时修改text属性值为1-855-XAMARIN:
![][109]
9.在工具箱中拖动一个 Button 到设计界面,并将其放置在 Plain Text 控件下面:
![][110]
10.选中 Button ,在属性面板中修改id属性值为@+id/TranslateButton,同时修改text属性值为Translate:
![][111]
11.下一步,再从工具箱中拖动第二个 Button 到设计界面,并将其放置于 Translate 按钮下面:
![][112]
12.选中新添加的 Button 控件,在属性面板中修改id属性值为@+id/CallButton,并将text属性值改为Call:
![][113]
按下CTRL+S键保存上述操作。
13.现在,我们添加转换电话号码的代码(从数字字母组合的号码到纯数字号码)。首先,我们添加一个新的文件:在解决方案管理器中,右击 Phoneword 项目,然后选择 添加—新建项...
![][114]
14.在 添加新项 对话框中,选择 Visual C# > Code 中的 Code File ,并将其命名为PhoneTranslator.cs:
注:最新中文版开发环境中,添加新项 下结构中,可以选择 Visual C#-代码 里的 代码文件,也可以选择 Visual C#-Android 里的 Class。—— 都是建立.cs文件
![][115]
15.移除掉模板代码,并用以下代码替换:
新版模板文件本身就是个空文件
using System.Text;
using System;
namespace Core
{
public static class PhonewordTranslator
{
public static string ToNumber(string raw)
{
if (string.IsNullOrWhiteSpace(raw))
return "";
else
raw = raw.ToUpperInvariant();
var newNumber = new StringBuilder();
foreach (var c in raw)
{
if (" -0123456789".Contains(c))
newNumber.Append(c);
else {
var result = TranslateToNumber(c);
if (result != null)
newNumber.Append(result);
}
// otherwise we've skipped a non-numeric char
}
return newNumber.ToString();
}
static bool Contains (this string keyString, char c)
{
return keyString.IndexOf(c) >= 0;
}
static int? TranslateToNumber(char c)
{
if ("ABC".Contains(c))
return 2;
else if ("DEF".Contains(c))
return 3;
else if ("GHI".Contains(c))
return 4;
else if ("JKL".Contains(c))
return 5;
else if ("MNO".Contains(c))
return 6;
else if ("PQRS".Contains(c))
return 7;
else if ("TUV".Contains(c))
return 8;
else if ("WXYZ".Contains(c))
return 9;
return null;
}
}
}
然后点击 文件-保存 (或按CTRL+S键)来保存PhoneTranslator.cs文件。
重新生成解决方案,以保证没有编译错误。
16.接下来,我们添加代码与UI界面连接起来。
双击解决方案管理器中的MainActivity.cs文件,打开后在MainActivity类中添加后台代码:
![][116]
17.首先配置 Translate 按钮。在MainActivity类中找到OnCreate方法。我们将会在OnCreate方法中的base.OnCreate(bundle)和SetContentView(Resource.Layout.Main)之后添加代码。移除模板按钮处理代码(没有就不用管),最后代码类似于下面示例:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Phoneword
{
[Activity (Label = "Phoneword", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Our code will go here
}
}
}
18.下一步,我们需要为 layout 文件中的控件(之前界面设计器中拖的控件)添加对应的引用。在OnCreate方法中添加如下代码:(添加在SetContentView之后)
// Get our UI controls from the loaded layout:
EditText phoneNumberText = FindViewById
Button translateButton = FindViewById
个人网站:http://www.shisujie.com

浙公网安备 33010602011771号