3.9 MonoForAndroid用户人机界面--调用另一个Activity

在Android的程序设计中,可在主程序里使用startActivity()这个方法来调用另一个Activity

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/black">
    <TextView
        android:id="@+id/text1"
        android:textSize="24sp"
        android:layout_width="186px"
        android:layout_height="29px"
        android:layout_x="70px"
        android:layout_y="32px"
        android:text="This is Activity 1" />
    <Button
        android:id="@+id/button1"
        android:layout_width="118px"
        android:layout_height="wrap_content"
        android:layout_x="100px"
        android:layout_y="82px"
        android:text="Go to Activity2" />
</AbsoluteLayout>

 myllayout.axml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/white">
    <TextView
        android:id="@+id/text2"
        android:textSize="24sp"
        android:layout_width="186px"
        android:layout_height="29px"
        android:layout_x="70px"
        android:layout_y="32px"
        android:textColor="@drawable/black"
        android:text="This is Activity 2" />
    <Button
        android:id="@+id/button2"
        android:layout_width="118px"
        android:layout_height="wrap_content"
        android:layout_x="100px"
        android:layout_y="82px"
        android:text="Go to Activity1" />
</AbsoluteLayout>

MainActivity.cs

using System;

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

namespace Ex03_09
{
    [Activity(Label = "Ex03_09", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        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.button1);

            button.Click += delegate { 
                Intent intent = new Intent(); 
                intent.SetClass(this, typeof(Activity1));
                StartActivity(intent);
                this.Finish();
            };


        }
    }
}

Activity1.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;

namespace Ex03_09
{
    [Activity(Label = "Activity1")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.mylayout);
            Button b2 = (Button)FindViewById(Resource.Id.button2);
            b2.Click += delegate {
                Intent intent = new Intent();
                intent.SetClass(this, typeof(MainActivity));
                StartActivity(intent);
                this.Finish();

            };
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值