Xamarin.Android(安卓)入门开发
16lz
2021-01-26
一,HelloWorld
这个阶段主要完成目标:Xamarin价格收费标准考察和分析。Xamarin.Android 部署安装。第一个Xamarin.Android工程HelloWorld,熟悉Android工程新建、运行、打包整个流程。第二个Xamarin.Android工程,一个手机拨号应用;完成对Xamarin.android 开发的基本了解。
阶段结论:Xamarin.android 支持Android基本开发。
注意事项:Xamarin.android 所打包比原生系统所打包 大3.02M
1.1 新建一个solution
1.2 运行工程
1.3 导出应用程序
1.3.1 选择工程
选中HelloWorld工程,在Tabbar栏,project–>Publish Android Application
1.3.2 填写签名证书
1.3.3 选择存放位置以及文件名,点击Create生成一个安装包
1.4 代码结构介绍
1.5 Xamarin Studio 介绍
二,手机拨号应用
2.1 整体界面效果
2.2 应用实现以及涉及知识点:
2.2.1 axml 界面布局使用
与Android 在eclipse中开发类似:允许使用界面布局选择器Toolbox将布局控件拖拽到页面上。也允许在Source中进行代码实现。
2.2.2 组件获取
Button mBtnCall = FindViewById<Button> (Resource.Id.CallButton); |
2.2.3 添加事件
mBtnCall.Click += (object sender, EventArgs e) =>{}; |
2.2.4 对话框
var callDialog = new AlertDialog.Builder(this);callDialog.SetMessage("Call " + translatedNumber + "?");callDialog.SetNeutralButton("Call", delegate { // do other thing});callDialog.SetNegativeButton("Cancel", delegate { });// Show the alert dialog to the user and wait for response.callDialog.Show(); |
2.2.5 页面跳转
var intent = new Intent(this, typeof(CallHistoryActivity));intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);StartActivity(intent); |
2.2.6 打印输出
Console.WriteLine ("Test Output Str"); |
2.2.7 新建类
右键工程,选择Add 可以新建以下类文件
三,附上主要代码类:
using System.Text;using System;namespace PhoneCallDemo{public static class PhonewordTranslator{public static string ToNumber(string raw){if (string.IsNullOrWhiteSpace(raw))return "";elseraw = 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;}}} |
using System;using Android.App;using Android.Content;using Android.Runtime;using Android.Views;using Android.Widget;using Android.OS;using System.Collections.Generic;namespace PhoneCallDemo{[Activity (Label = "PhoneCall", MainLauncher = true, Icon = "@drawable/icon")]public class MainActivity : Activity{static readonly List<string> phoneNumbers = new List<string>();protected override void OnCreate (Bundle bundle){base.OnCreate (bundle);// Set our view from the "main" layout resourceSetContentView (Resource.Layout.Main);// Get our button from the layout resource,// and attach an event to itEditText mEditPhoneNum = FindViewById<EditText> (Resource.Id.PhoneNumberText);Button mBtnCall = FindViewById<Button> (Resource.Id.CallButton);Button mBtnTrans = FindViewById<Button> (Resource.Id.TranslateButton);Button mBtnCallHistory = FindViewById<Button> (Resource.Id.callhistoryButton);mBtnCall.Enabled = false;mBtnCallHistory.Enabled = false;string translatedNumber = string.Empty;mBtnTrans.Click += (object sender, EventArgs e) => {translatedNumber = PhoneCallDemo.PhonewordTranslator.ToNumber(mEditPhoneNum.Text);if (String.IsNullOrWhiteSpace(translatedNumber)){mBtnCall.Text="Call";mBtnCall.Enabled = false;}else{mBtnCall.Text = "Call " + translatedNumber;mBtnCall.Enabled = true;}};mBtnCall.Click += (object sender, EventArgs e) => {var callDialog = new AlertDialog.Builder(this);callDialog.SetMessage("Call " + translatedNumber + "?");callDialog.SetNeutralButton("Call", delegate {phoneNumbers.Add(translatedNumber);mBtnCallHistory.Enabled = true;// Create intent to dial phonevar callIntent = new Intent(Intent.ActionCall);callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));StartActivity(callIntent);});callDialog.SetNegativeButton("Cancel", delegate { });// Show the alert dialog to the user and wait for response.callDialog.Show();};mBtnCallHistory.Click += (sender, e) =>{var intent = new Intent(this, typeof(CallHistoryActivity));intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);StartActivity(intent);};}}} |
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Android.App;using Android.Content;using Android.OS;using Android.Runtime;using Android.Views;using Android.Widget;namespace PhoneCallDemo{[Activity (Label = "CallHistoryActivity")]public class CallHistoryActivity : ListActivity{protected override void OnCreate (Bundle bundle){base.OnCreate (bundle);// Create your application herevar phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);}}} |
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/PhoneNumberText" android:hint="TEL:123456" /> <Button android:id="@+id/TranslateButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text_translate" /> <Button android:text="Call" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/CallButton" /> <Button android:text="@string/CallHistory" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/callhistoryButton" /></LinearLayout> |
下一个阶段:Xamarin.IOS 部署开发
更多相关文章
- Android音频开发之使用OpenSL ES API
- Android(安卓)Studio 插件开发入门
- android开发技术
- 快应用 QuickApp:配置开发环境 和 第一个工程 HelloWorld
- Android前景,前途
- Android系统体系概要
- Android应用开发中使用GridView网格布局的代码示例
- iOS开发-UITableView常用方法
- android系统 在jack-server下 生成 jar 用于android app工程