本文将直接解释我写的一个示例。示例目的在于展示 Android 活动在 Xamarin 中的用法。如果有朋友对基础知识不太了解建议先学 Android 。

  新建一个 Xamarin.Android 项目 ActivityStates。在项目中新建文件夹 Activities 用来存放活动。项目结构如下

  (GitHub : https://github.com/ZhangGaoxing/xamarin-android-demo/tree/master/ActivityStates

  在 Activities 文件夹中新建两个活动 DialogActivity 和 NormalActivity(右击 Activities —— 添加 —— 新建项 —— 活动)

  在 Resources 下的 layout 文件夹中新建两个与之相对应的布局 DialogLayout.axml 和 NormalLayout.axml(右击 layout —— 添加 —— 新建项 —— Android 布局)

  完成后项目结构如下图所示

  说完活动、布局的新建,接下来开始编辑代码,新建的活动和布局的代码很简单

  NormalActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace ActivityStates.Activities
{
[Activity(Label = "NormalActivity")]
public class NormalActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.NormalLayout);
}
}
}

  对应的 NormalLayout.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="This is a Normal Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

  下面要说说 DialogActivity.cs 。由于我们需要把当前的活动主题改为 Dialog 形式,按照一般的 Android 项目,我们应该编辑 AndroidManifest.xml 文件,但在 Xamarin 项目中采用 C# 中的特性(Attribute)来进行配置。详细说明可以按 F12 查看代码。

  所以在配置活动主题时我们需要把活动特性修改为以下代码:[Activity(Label = "DialogActivity", Theme ="@android:style/Theme.Dialog")]

  DialogActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace ActivityStates.Activities
{
[Activity(Label = "DialogActivity", Theme ="@android:style/Theme.Dialog")]
public class DialogActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.DialogLayout);
}
}
}

  DialogLayout.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="This is a Dialog Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

  

  下面来说明一下主活动和主布局。在主布局中我们需要两个 Button 用来跳转到上面两个新建的活动,和一个 TextView 用来输出 Log。

  Main.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Dialog Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDialog" />
<Button
android:text="Normal Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnNormal" />
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtLog" />
</LinearLayout>

  而在主活动中我们需要重写活动生命周期的7个方法,在 OnDestroy() 方法中,由于活动已经销毁,向 TextView 中输出完全没有意义,所以采用 Toast 通知的方法来输出。

  MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content; namespace ActivityStates
{
[Activity(Label = "ActivityStates", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
TextView txtLog; // 用于显示信息 protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
// 获取控件
Button btnDialog = FindViewById<Button>(Resource.Id.btnDialog);
Button btnNormal = FindViewById<Button>(Resource.Id.btnNormal);
txtLog = FindViewById<TextView>(Resource.Id.txtLog);
// 输出信息
txtLog.Text += "OnCreate()\n"; btnDialog.Click += (sender, e) =>
{
Intent dialog = new Intent(this, typeof(Activities.DialogActivity));
StartActivity(dialog);
}; btnNormal.Click += (sender, e) =>
{
Intent normal = new Intent(this, typeof(Activities.NormalActivity));
StartActivity(normal);
};
} protected override void OnStart()
{
base.OnStart();
txtLog.Text += "OnCreate()\n";
} protected override void OnResume()
{
base.OnResume();
txtLog.Text += "OnResume()\n";
} protected override void OnPause()
{
base.OnPause();
txtLog.Text += "OnPause()\n";
} protected override void OnStop()
{
base.OnStop();
txtLog.Text += "OnStop()\n";
} protected override void OnRestart()
{
base.OnRestart();
txtLog.Text += "OnRestart()\n";
} protected override void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "OnDestroy()", ToastLength.Short).Show();
}
}
}

  运行图

最新文章

  1. ASP.NET SignalR入门
  2. apt-get install *** 出现 软件包***没有提供可供安装的候选者
  3. iOS - Swift 与 Objective-C 互相操作
  4. Android应用程序窗口(Activity)实现框架简要介绍和学习计划
  5. C# for循环 迭代法 穷举法应用
  6. Spring Cloud官方文档中文版-服务发现:Eureka服务端
  7. Linux.SSH.修改SSH端口号
  8. Docker镜像细节
  9. C# socket通讯 send方法记录
  10. hiho1258 Osu! Master
  11. Spring中 PROPAGATION_REQUIRED 解释 事物是在一个方法里调用其他的方法,一起成功或者一起失败,是方法之间的关系,而不是某一个方法内部的问题。而且要以抛异常的方式来表明方法的失败,以此来导致事物起作用,大家全失败。
  12. Linux脚本程序
  13. [POI2015]LOG(树状数组)
  14. OSGI企业应用开发(九)整合Spring和Mybatis框架(二)
  15. bzoj 4033 树上染色 - 树形动态规划
  16. Visual Studio 项目模板制作(二)
  17. (连通图 ) Redundant Paths --POJ --3177
  18. Solidity两个string的比较
  19. sqlmap注入分类
  20. 路由器终端常用linux命令汇总(持续更新)

热门文章

  1. 团队作业1--团队展示&amp;选题(SNS)
  2. 201521123008《Java程序设计》第八周实验总结
  3. 201521123039 《java程序设计》第四周学习总结
  4. 社工数据搜索引擎搭建 - Build Social Engineer Evildata Search Engine
  5. ubuntu下php不能显示中文的问题的解决过程。
  6. 201521123007《Java程序设计》第9周学习总结
  7. 201521123055 《Java程序设计》第12周学习总结
  8. 201521123002《Java程序设计》第14周学习总结
  9. Java程序设计——学生信息系统
  10. 04面向对象编程-02-原型继承 和 ES6的class继承