第二课 两个视图+数据传输

项目结构如下图:

image

比第一课多了一个活动(Activity)。

1.Activity1.cs代码如下:

using System;
 
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
 
namespace MyLesson
{
    [Activity(Label = "Lesson2_View1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Lesson21 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            LinearLayout layout = new LinearLayout(this);
            layout.Orientation = Orientation.Vertical;
 
            TextView tv = new TextView(this);
            tv.Text = "这是第一个视图";
 
            Button b = new Button(this);
            b.Text = "点击切换到第二个视图";
            b.Click += b_Click;
 
            layout.AddView(tv);
            layout.AddView(b);
            SetContentView(layout);
        }
 
        void b_Click(object sender, EventArgs e)
        {
            Intent intent = new Android.Content.Intent(this, typeof(Lesson22));
            intent.PutExtra("MessageName", "你好,这里是视图1");
            StartActivity(intent);
        }
    }
}
 

2.Activity2.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 MyLesson
{
    [Activity(Label = "Lesson2_view2")]
    public class Lesson22 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            LinearLayout layout = new LinearLayout(this);
            layout.Orientation = Orientation.Vertical;
 
            TextView tv = new TextView(this);
            tv.Text = "这是第二个视图,来自第一个视图的数据是:" + (Intent.GetStringExtra("MessageName") ?? "没说什么");
 
 
            layout.AddView(tv);
            
            SetContentView(layout);
        }
    }
}

你可能感兴趣的:(视图)