在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();
};
}
}
}