Mono for Android: 利用mono for android开发的第一个程序

装完mono之后,在弄monocross项目之前,先试了下直接用mono for Android开发Android应用。

1、创建一个mono for Android application。

右击项目,选择options,可以找到我们之前的AndroidManifest:

创建之后的目录架构如下:

弄过Android的一定不会陌生啦,assets:二进制资源文件;Resource:资源文件包;

其中的Activity1.cs就是我们的各个View的Controller了。

看里面的东西:Activity1.cs

复制代码
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace monoforandroidFirst
{
    [Activity (Label = "monoforandroidFirst", MainLauncher = true)]
    public class Activity1 : Activity
    {
        int count = 1;
        string text = null;

        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
            Button button = FindViewById<Button> (Resource.Id.myButton);
            
            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
                text = button.Text;
            };

            Button btn = FindViewById<Button>(Resource.Id.toView2Btn);
            btn.Click += btnHandle;
        }

        void btnHandle(object sender, EventArgs e){

            // 创建 Intent 并传递用户输入的信息
            var intent = new Intent(this,typeof(Activity2));
            intent.PutExtra("btn clicks","had click "+text+"times!");
            // 启动第二个 Activity
            this.StartActivity(intent);
        }
    }
}
复制代码

[Activity (Label = "monoforandroidFirst", MainLauncher = true)]   // 首次启动界面

SetContentView (Resource.Layout.Main);          // 绑定View

其他的语法也很容易可以看懂。

 

我的程序是在有两个页面,第一个有2个Btn,一个点击累加次数,第二个点击跳转到第二个View,第二个view显示第一个View的点击次数。

看效果图:

                                       

 

直接新建一个activity+一个Layout。因为太简单了,稍微动Android的都可以很快搞定,就不在赘述。

 

小结:

mono for android 对于.net 开发者来说还是不错的,可以很快上手,并且有很多现有的.net代码可以利用。对于已经掌握Android的开发的来说,上手也十分简单,熟悉C#语言就行了。

 

本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/11/15/2770869.html,如需转载请自行联系原作者