(4)鼠标右击项目名,选择【添加…】à【新建项】,在弹出的窗口中,选择“Activity”模板,文件名:CallHistoryActivity.cs,单击【添加】。然后将该文件改为下面的内容(省略了using……):
namespace PhonewordApp
{
[Activity(Label = "CallHistoryActivity")]
public class CallHistoryActivity : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var phoneNumbers =
Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];
this.ListAdapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleListItem1, phoneNumbers);
}
}
}
其中,c = a??b; 的含义相当于:if (a != null ){ c = a;} else { c = b;}
(5)修改MainActivity.cs文件,目标是收集第1个屏幕界面运行时拨打过的所有电话号码,并将其在第2个屏幕上显示出来。在MainActivity.cs文件中添加下面的代码:
……
using System.Collections.Generic;
namespace E01PhonewordApp
{
[Activity(Label = "E01PhonewordApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
static readonly List<string> phoneNumbers = new List<string>();
protected override void OnCreate(Bundle bundle)
{
……
var buttonCallHistory =
FindViewById<Button>(Resource.Id.buttonCallHistory);
buttonCallHistory.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(CallHistoryActivity));
intent.PutStringArrayListExtra("phone_numbers",
phoneNumbers);
StartActivity(intent);
};
buttonCall.Click += (s, e) =>
{
phoneNumbers.Add(translatedNumber);
buttonCallHistory.Enabled = true;
// 当单击【拨号】时,尝试拨号
var callDialog = new AlertDialog.Builder(this);
callDialog.SetMessage("播出号码:" + translatedNumber +
",拨号吗?");
callDialog.SetNeutralButton("拨号", delegate
{
// Create intent to dial phone
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" +
translatedNumber));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("取消", delegate { });
callDialog.Show();
};
}
}
}
(6)重新生成项目,确保无错误。
(7)运行,再拨一个号(例如12345678901),然后查看拨号记录。下图是用另一种模拟器查看的运行效果(你可以创建多种不同的模拟器,分别观察同一个项目的运行效果):

到这里,我们就完成了用C#编写的第1个Android应用程序。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。










