Mono for Android—初體驗之「電話撥號器」

一、Main.axml文件:android

<?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:inputType="phone"
        android:layout_width="match_parent"   //填充父控件大小
        android:layout_height="wrap_content"  //根據內容自動拉伸
        android:id="@+id/etphoneNum" />
    <Button
        android:text="Button"
        android:layout_width="100.0dp"  //dp,與密度無關的像素,一種基於屏幕密度的抽象單位。在每英寸160點的顯示器上,1dp="1px";
        android:layout_height="wrap_content"
        android:id="@+id/btnSend"
        android:layout_marginRight="0.0dp" />
</LinearLayout>ide

二、Activity1.cs文件:ui

namespace AndroidDemo {     this

/// <summary>     spa

/// 標籤AndroidDemo將出如今Android應用程序窗口中;MainLauncher批註,其代表了Activity是應用程序的主要啓動對象;Icon是程序圖標     xml

/// </summary>     對象

[Activity(Label = "電話撥號器", MainLauncher = true, Icon = "@drawable/icon")]     utf-8

public class Activity1 : Activity  //activity活動,一個「活動」就是一個用來完成單個任務有的用戶界面組件     資源

{         get

        EditText etphoneNum;        

         Button btnSend;        

      protected override void OnCreate(Bundle bundle)

     {            

            base.OnCreate(bundle);//初始化資源包

            // Set our view from the "main" layout resource            

           SetContentView(Resource.Layout.Main);//加載視圖

            btnSend = this.FindViewById<Button>(Resource.Id.btnSend);            

            etphoneNum = this.FindViewById<EditText>(Resource.Id.etphoneNum);

            btnSend.Click += btnSend_Click;

        }

        void btnSend_Click(object sender, EventArgs e)        

       {            

            string phoneNum = etphoneNum.Text.Trim();            

           if (phoneNum.Length == 0)

          {                

                     Toast.MakeText(this, "請輸入手機號", ToastLength.Long).Show();                

                     return;            

            }            

          //Intent myIntent = new Intent();//建立一個意圖            

         //myIntent.SetAction(Intent.ActionCall);//設置行爲是「打電話」            

         //myIntent.SetData(Android.Net.Uri.Parse("tel:"+phoneNum));//設置數據,數據即爲電話號碼,注意格式:tel:123456***            

          var myIntent = new Intent(Intent.ActionCall,Android.Net.Uri.Parse("tel:"+phoneNum));

            StartActivity(myIntent);//開始活動        

        }    

   }

}

三、最後,別忘設置權限,只有擁用CALL_PHONE權限才能實現撥打電話

 能夠右擊「項目」,選擇「屬性」,便可打開Android Manifest清單,在Required permissions下選擇「CALL_PHONE」便可。

<users-permission android:name="android.permission.CALL_PHONE"/>

相關文章
相關標籤/搜索