zoukankan      html  css  js  c++  java
  • Xamarin 编程之打电话

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

    Main.axml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     android:minWidth="25px"
     7     android:minHeight="25px">
     8     <TextView
     9         android:text="{0} Calling"
    10         android:textAppearance="?android:attr/textAppearanceLarge"
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:id="@+id/lblNote"
    14         android:textAlignment="center" />
    15     <EditText
    16         android:inputType="phone"
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:id="@+id/txtPhoneNumber" />
    20     <Button
    21         android:text="Call"
    22         android:layout_width="match_parent"
    23         android:layout_height="wrap_content"
    24         android:id="@+id/btnCall" />
    25 </LinearLayout>
    View Code

    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

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 using Android.App;
     7 using Android.Content;
     8 using Android.OS;
     9 using Android.Runtime;
    10 using Android.Views;
    11 using Android.Widget;
    12 
    13 using System.Text.RegularExpressions;
    14 
    15 namespace PhoneCall
    16 {
    17     public class Phone
    18     {
    19         public Phone()
    20         {
    21 
    22         }
    23 
    24         /// <summary>
    25         /// 验证
    26         /// </summary>
    27         /// <param name="number"></param>
    28         /// <returns></returns>
    29         public static bool Check(string number)
    30         {
    31             if (Regex.IsMatch(number, @"1[3|5|7|8|][0-9]{9}"))
    32                 return true;
    33             return false;
    34         }
    35 
    36         /// <summary>
    37         /// 电话呼叫
    38         /// </summary>
    39         /// <param name="number"></param>
    40         /// <returns></returns>
    41         public static Intent Call(string number)
    42         {
    43             Intent _it = new Intent(Intent.ActionCall);
    44             _it.SetData(Android.Net.Uri.Parse("tel:"+number));
    45             return _it;
    46         }
    47     }
    48 }
    View Code

    问题:

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="PhoneCall.PhoneCall" android:versionCode="1" 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>

    效果

  • 相关阅读:
    Python面向对象(组合、菱形继承、多态)
    Python面向对象
    Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识
    python正则re模块
    Python序列化、date、random、os模块
    Python包和日志模块
    python面向对象、模块讲解
    python递归函数、二分法、匿名函数、(sorted、map、filter内置函数应用)
    Python生成器、三元表达式、列表生成式、字典生成式、生成器表达式
    Python迭代器
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/4371350.html
Copyright © 2011-2022 走看看