1.Main.axml

 <?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"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="请输入电话号码:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Tag" />
<EditText
android:text="13002929017"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/PhoneNumberText" />
<Button
android:text="转化"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TranslateButton" />
<Button
android:text="呼叫"
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"
android:enabled="false" />
</LinearLayout>

2.MainActivity.cs

 using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS; namespace Phoneword_Droid
{
[Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = ;
static readonly List<string> phoneNumbers = new List<string>(); protected override void OnCreate(Bundle bundle)
{
//button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
Button callButton = FindViewById<Button>(Resource.Id.CallButton);
Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); callButton.Enabled = false; #region//转化拨号
string translatedNumber = string.Empty;
translateButton.Click += (object sender, EventArgs e) =>
{
translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text);
if (String.IsNullOrWhiteSpace(translatedNumber))
{
callButton.Text = "Call";
callButton.Enabled = false;
}
else
{
callButton.Text = "Call" + translatedNumber;
callButton.Enabled = true;
}
};
#endregion #region//拨打电话
callButton.Click += (s, e) =>
{
//对话框
var callDialog = new AlertDialog.Builder(this); //对话框内容
callDialog.SetMessage("Call" + translatedNumber + "?"); //拨打按钮
callDialog.SetNeutralButton("Call", delegate
{
////使用意图拨打电话
//var callIntent = new Intent(Intent.ActionCall); ////将需要拨打的电话设置为意图的参数
//callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); //StartActivity(callIntent); //将电话加入到历史记录列表中
phoneNumbers.Add(translatedNumber); //如果callHistoryButton的定义在这段代码后面将会出错,所以我们这个时候需要将
//Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代码提前
callHistoryButton.Enabled = true; //使用意图拨打电话
var callIntent = new Intent(Intent.ActionCall); //将需要拨打的电话设置为意图的参数
callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); StartActivity(callIntent); }); //取消按钮
callDialog.SetNegativeButton("Cancel", delegate { }); //显示对话框
callDialog.Show();
};
#endregion #region//通话记录
callHistoryButton.Click += (e, t) =>
{
//指定意图需要打开的活动
var intent = new Intent(this, typeof(CallHistoryActivity));
//设置意图传递的参数
intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
StartActivity(intent);
};
#endregion }
}
}

3.PhoneTranslator.cs

 using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS; namespace Phoneword_Droid
{
[Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = ;
static readonly List<string> phoneNumbers = new List<string>(); protected override void OnCreate(Bundle bundle)
{
//button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
Button callButton = FindViewById<Button>(Resource.Id.CallButton);
Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); callButton.Enabled = false; #region//转化拨号
string translatedNumber = string.Empty;
translateButton.Click += (object sender, EventArgs e) =>
{
translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text);
if (String.IsNullOrWhiteSpace(translatedNumber))
{
callButton.Text = "Call";
callButton.Enabled = false;
}
else
{
callButton.Text = "Call" + translatedNumber;
callButton.Enabled = true;
}
};
#endregion #region//拨打电话
callButton.Click += (s, e) =>
{
//对话框
var callDialog = new AlertDialog.Builder(this); //对话框内容
callDialog.SetMessage("Call" + translatedNumber + "?"); //拨打按钮
callDialog.SetNeutralButton("Call", delegate
{
////使用意图拨打电话
//var callIntent = new Intent(Intent.ActionCall); ////将需要拨打的电话设置为意图的参数
//callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); //StartActivity(callIntent); //将电话加入到历史记录列表中
phoneNumbers.Add(translatedNumber); //如果callHistoryButton的定义在这段代码后面将会出错,所以我们这个时候需要将
//Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代码提前
callHistoryButton.Enabled = true; //使用意图拨打电话
var callIntent = new Intent(Intent.ActionCall); //将需要拨打的电话设置为意图的参数
callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber)); StartActivity(callIntent); }); //取消按钮
callDialog.SetNegativeButton("Cancel", delegate { }); //显示对话框
callDialog.Show();
};
#endregion #region//通话记录
callHistoryButton.Click += (e, t) =>
{
//指定意图需要打开的活动
var intent = new Intent(this, typeof(CallHistoryActivity));
//设置意图传递的参数
intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
StartActivity(intent);
};
#endregion }
}
}

4.CallHistoryActivity.cs

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 Phoneword_Droid
{
[Activity(Label = "@string/callHistory")]
public class CallHistoryActivity : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//从意图中获取传递过来的参数
var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0]; //将字符串数组显示到列表控件中(因为继承的是ListActivity所以整个视图就是一个列表)
this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers); //关于ArrayAdapter的第二个参数,其实就是指定列表中每个项的视图,后面我们会通过自定义的方式控制列表的项 }
}
}

模拟器

运行效果

Xamarin.Android 入门实例(3)之呼叫电话号码的更多相关文章

  1. Xamarin.Android 入门实例(4)之实现对 SQLLite 进行添加/修改/删除/查询操作

    1.Main.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  2. Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供

    1.WCF 契约 ICalculator.cs using System.ServiceModel; namespace Contracts { [ServiceContract] public in ...

  3. Xamarin.Android 入门实例(1)之获取与解析JSON

    1.Main.axml 视图界面 2.视图代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  4. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  5. Xamarin.Android 入门之:Android API版本设置

    一.引言 Xamarin.Android有几个Android API级别设置,确定多个版本的Android应用程序的兼容性.本博客解释了这些设置意味着什么,如何配置它们,以及它们在运行时对您的应用程序 ...

  6. VS/Xamarin Android入门一

    一.安装和配置(以Visual Studio Pro 2015为例) Visual Studio2015直接提供了这个插件的选择项,稍微提示一下,如果要安装的话,最好准备好十个小时的打算,而且是网速不 ...

  7. Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片

    一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...

  8. Xamarin.Android 入门之:Listview和adapter

    一.引言 不管开发什么软件,列表的使用是必不可少的,而本章我们将学习如何使用Xamarin去实现它,以及如何使用自定义适配器.关于xamarin中listview的基础和适配器可以查看官网https: ...

  9. Xamarin.Android 入门之:Android的生命周期

    一.前言 活动是Android应用程序的基本构建块,他们可以在许多不同的状态存在.当你把一个Android程序置于后台,过一段时间再打开发现之前的数据还存在. 二.活动状态 下面的图表说明了一个活动可 ...

随机推荐

  1. linux命令笔记之ls

    假设要将全部的命令以一篇博客持续更新的方式去展现,将来在查找的时候非常不方便.出于这种考虑.将来将非常多命令都分开记录. 这里,一些基础使用方法都不做太多说明.主要记录下平时经经常使用到的一些命令. ...

  2. .Net——使用.net内置处理程序处理自己定义节点Demo

    在.net中.由于对不同的节点,都相应着类去对它进行处理,.net里面为了方便.已经内置了一些类供我们使用.使我们在读取配置文件时.不必自己去定义类去处理自己定义的自己定义节点. 以下我们写了这样一个 ...

  3. Cocos2d-x串算出Size方法

    项目需要,根据所输入的字符串,的需要计算串帐户Size. 包代码如下面.只需要传递一个字符串,您可以返回Size: Size ChartDemoScene::calculateFontSize(con ...

  4. shortcut switch in terminal start pos & end pos

    ctrl a ctrl e switch in terminal start pos & end pos

  5. Do you master on array in C ?

    Do you master on array in C ? 因为新标准C99的支持变长数组, 差点儿C的标准特性就是看着gcc来的(Linux 内核严重依赖GCC) int mani() { cons ...

  6. asp.net出现正在中止线程解决方案

    刚才又再次遇到了一个之前遇到的问题,在这里记录一下. 起因: 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAb ...

  7. Enum实现单例模式

    package com.wjy.effective; public enum Singleton { INSTANCE; private int numa; private int numb; pub ...

  8. A Game of Thrones(9) - Tyrion

    Somewhere in the great stone maze(迷宫:迷惑) of Winterfell, a wolf howled. The sound hung over the castl ...

  9. Fashion Meets Finance聚会来袭-7月19日 北京

    http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NjEzMjMyMQ%3D%3D&appmsgid=10000704&itemidx= ...

  10. Android开源项目pulltorefresh分析与简单使用

    在Android开发中有时我们须要訪问网络实时刷新数据.比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等.android-pul ...