Mono for Android—初体验之“电话拨号器”

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">
    <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>

2、Activity1.cs文件:

namespace AndroidDemo {    

/// <summary>    

/// 标签AndroidDemo将出现在Android应用程序窗口中;MainLauncher批注,其表明了Activity是应用程序的主要启动对象;Icon是程序图标    

/// </summary>    

[Activity(Label = "电话拨号器", MainLauncher = true, Icon = "@drawable/icon")]    

public class Activity1 : Activity  //activity活动,一个“活动”就是一个用来完成单个任务有的用户界面组件    

{        

        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);//开始活动        

        }    

   }

}

3、最后,别忘设置权限,只有拥用CALL_PHONE权限才能实现拨打电话

 可以右击“项目”,选择“属性”,即可打开Android Manifest清单,在Required permissions下选择“CALL_PHONE”即可。

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

posted @ 2013-12-01 19:58  霄嵩  阅读(803)  评论(0编辑  收藏  举报