Xamarin 编程之打电话

时间:2022-09-29 16:23:51

参考:http://www.cnblogs.com/yaozhenfa/

Xamarin 编程之打电话

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="{0} Calling"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblNote"
android:textAlignment="center" />
<EditText
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtPhoneNumber" />
<Button
android:text="Call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnCall" />
</LinearLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Thinks">http://www.cnblogs.com/yaozhenfa/p/xamarin_android_quickstart.html</string>
<string name="Project">CallPhone</string>
<string name ="Title">Calling</string>
<string name="Messages">Call {0} ?</string>
<string name="Warnning_Title">Information</string>
<string name="Warnning_Value">Couldn`t be Empty!</string>
<string name="Warnning_IllegalValue">Illegal CallPhone Number!</string>
</resources>

MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS; namespace PhoneCall
{
[Activity(Label = "PhoneCall", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private string _note;
private EditText txtPhoneNumber;
private TextView lblNote;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); // Get our button from the layout resource,
// and attach an event to it
///根据ID找手机号输入框按钮
txtPhoneNumber = FindViewById(Resource.Id.txtPhoneNumber) as EditText;
lblNote = FindViewById<TextView>(Resource.Id.lblNote);
txtPhoneNumber.SetTextColor(Android.Graphics.Color.LightGreen);
txtPhoneNumber.AfterTextChanged += txtPhoneNumber_AfterTextChanged;
_note = lblNote.Text;
txtPhoneNumber_AfterTextChanged(txtPhoneNumber, null);
///根据ID找Call按钮
Button btnCall = FindViewById<Button>(Resource.Id.btnCall);
///为btnCall注册点击事件(Lambda)
btnCall.Click += (sender, e) =>
{
///设置MessageBox
AlertDialog _ad = new AlertDialog.Builder(this).Create();
///Check
if (string.IsNullOrWhiteSpace(txtPhoneNumber.Text))
{
_ad.SetTitle(Resource.String.Warnning_Title);
_ad.SetMessage(GetString(Resource.String.Warnning_Value));
_ad.SetButton("Ok", delegate { });
_ad.Show();
return;
}
///Confirm
if (!Phone.Check(txtPhoneNumber.Text))
{
txtPhoneNumber.Text = "";
_ad.SetTitle(Resource.String.Warnning_Title);
_ad.SetMessage(GetString(Resource.String.Warnning_IllegalValue));
_ad.SetButton("Ok", delegate { });
_ad.Show();
return;
}
_ad.SetTitle(Resource.String.Title);
_ad.SetMessage(string.Format(GetString(Resource.String.Messages), txtPhoneNumber.Text));
_ad.SetButton("Ok", new EventHandler<DialogClickEventArgs>(Call));
_ad.SetButton2("Cancel", delegate { });
_ad.Show();
}; }
private void Call(object sender, DialogClickEventArgs e)
{
StartActivity(Phone.Call(txtPhoneNumber.Text));
} void txtPhoneNumber_AfterTextChanged(object sender, Android.Text.AfterTextChangedEventArgs e)
{
lblNote.Text = string.Format(_note, (sender as EditText).Text);
}
}
}

Phone.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; using System.Text.RegularExpressions; namespace PhoneCall
{
public class Phone
{
public Phone()
{ } /// <summary>
/// 验证
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool Check(string number)
{
if (Regex.IsMatch(number, @"1[3|5|7|8|][0-9]{9}"))
return true;
return false;
} /// <summary>
/// 电话呼叫
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static Intent Call(string number)
{
Intent _it = new Intent(Intent.ActionCall);
_it.SetData(Android.Net.Uri.Parse("tel:"+number));
return _it;
}
}
}

问题:

Xamarin 编程之打电话

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="PhoneCall.PhoneCall" android:versionCode="" android:versionName="1.0" android:installLocation="auto">
<uses-sdk />
<application android:label="PhoneCall" android:icon="@drawable/Icon"></application>
<uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>

Xamarin 编程之打电话Xamarin 编程之打电话

效果

Xamarin 编程之打电话Xamarin 编程之打电话