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


    项目结构如下图:

    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);
            }
        }
    }
  • 相关阅读:
    【脑图】iOS的Crash分类和捕获
    Ruby03
    Ruby02
    Ruby01
    如何快速把一个十进制数转换为二进制?
    iOS
    互联网协议基本知识
    XCBB
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/catzhou/p/3579972.html
Copyright © 2020-2023  润新知