Xamarin.Android之转换,呼叫,查看历史纪录

E文文章。

功能:能将输入的字母转换成相应的数字。并且能呼叫出去。能查看呼叫的历史纪录。

界面代码如下:

 <?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="fill_parent"
android:layout_height="wrap_content"
android:text="0712-XAMARIN"
android:id="@+id/et"
/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="转换"
android:id="@+id/btnTran"
/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="呼叫"
android:id="@+id/btnCall"
/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="历史纪录"
android:id="@+id/btnCallHistory"
/> </LinearLayout>

主Activity代码。

 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 App3
{
[Activity(Label = "CallActivity", MainLauncher = true, Icon = "@drawable/icon2")]
public class CallActivity : Activity
{
//定义手机集合。
private static readonly List<string> PhoneNumbers = new List<string>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Call);
EditText et = FindViewById<EditText>(Resource.Id.et);
Button btnTran = FindViewById<Button>(Resource.Id.btnTran);
Button btnCall = FindViewById<Button>(Resource.Id.btnCall);
btnCall.Enabled = false;
Button btnCallHistory = FindViewById<Button>(Resource.Id.btnCallHistory);
btnCallHistory.Enabled = false;
string translatedNumber = string.Empty; btnTran.Click += (sender, e) =>
{
translatedNumber = PhoneTranslator.ToNumber(et.Text);
//将转换的手机号加入到手机集合中。
PhoneNumbers.Add(translatedNumber);
btnCallHistory.Enabled = true;
if (String.IsNullOrWhiteSpace(translatedNumber))
{
btnCall.Text = "呼叫";
btnCall.Enabled = false;
}
else
{
btnCall.Text = "呼叫" + translatedNumber;
btnCall.Enabled = true;
}
}; btnCall.Click += (sender, e) =>
{
//对话框
var callDialog = new AlertDialog.Builder(this);
callDialog.SetMessage("呼叫" + translatedNumber + "?");
//拨打按钮
callDialog.SetNeutralButton("呼叫", delegate
{
//使用意图拨打电话
var callIntent = new Intent(Intent.ActionCall);
//将需要拨打的电话设置为意图的参数.注意写法
callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("取消", delegate { });
callDialog.Show();
}; btnCallHistory.Click += (sender, e) =>
{
//用意图打开历史纪录的活动
Android.Content.Intent it = new Intent(this, typeof(CallHistoryActiviry));
it.PutStringArrayListExtra("phoneNumbers", PhoneNumbers);
StartActivity(it);
}; }
}
}

通话纪录的Activity代码。

 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 App3
{
[Activity(Label = "CallHistoryActiviry")]
public class CallHistoryActiviry : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var phoneNumbers = Intent.Extras.GetStringArrayList("phoneNumbers") ?? new string[];
//只有当此Activity继承于ListActivity时,整个视图才是列表,才可以这么写。
this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1, phoneNumbers);
}
}
}
04-11 07:56