分类:C#、Android、VS2015;
创建日期:2016-02-17
一、简介
上一讲已经说过,系统升级为Win10后,重新创建了一个新的项目:MyDemos,并把前7章合并到了这个项目中,这次简单介绍一下合并后的项目结构以及主界面相关的代码。
二、项目结构
创建的MyDemos项目结构截图如下:
这些资源文件和活动文件的创建办法见前面章节的介绍,此处不再重复。
三、主界面相关的代码
1、修改Main.axml文件
将该文件改为下面的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/heading1"
android:text="用C#和VS2015编写Android手机App"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:textSize="16dp"
android:textColor="#FFFF0000"
android:textStyle="bold"
android:padding="5dp"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/heading2"
android:text="www.cnblogs.com/rainmj/"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:textSize="14dp"
android:textColor="#FF267F00"
android:textStyle="bold"
android:gravity="center_horizontal"
android:paddingBottom="5dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FF909090"
android:layout_marginBottom="5dp" />
<ExpandableListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expandableListView1" />
</LinearLayout>
2、添加MyApplication.cs文件
在项目的根目录下添加该文件,然后将该文件改为下面的内容:
using System;
using Android.App;
using Android.Runtime;
using Com.Baidu.Mapapi;
namespace MyDemos
{
[Application]
public class MyApplication : Application
{
//必须提供此构造函数
public MyApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
} public override void OnCreate()
{
base.OnCreate();
SDKInitializer.Initialize(ApplicationContext);
}
}
}
3、添加MyBroadcastReceiver.cs文件
在项目的根目录下添加该文件,然后将该文件改为下面的内容:
using Android.Content;
using Android.Widget;
using Com.Baidu.Mapapi;
using Android.Util; namespace MyDemos
{
[BroadcastReceiver]
public class MyBroadcastReceiver : BroadcastReceiver
{
private MainActivity mainActivity; //必须提供无参数的构造函数
public MyBroadcastReceiver() { } public MyBroadcastReceiver(MainActivity bdMapMain)
{
mainActivity = bdMapMain;
} public override void OnReceive(Context context, Intent intent)
{
string s = intent.Action;
TextView text = mainActivity.FindViewById<TextView>(Resource.Id.heading2);
switch (s)
{
case SDKInitializer.SdkBroadtcastActionStringPermissionCheckError:
text.Text= "Andoid SDK(v3.7.1)key 验证出错!";
break;
case SDKInitializer.SdkBroadtcastActionStringPermissionCheckOk:
Log.Debug("MainActivity", "百度Andoid SDK(v3.7.1)key 验证成功!");
break;
case SDKInitializer.SdkBroadcastActionStringNetworkError:
text.Text = "Andoid SDK(v3.7.1)key 验证失败:网络出错!";
break;
}
}
}
}
4、添加MyExpandableListAdapter.cs文件
在项目的根目录下添加该文件,然后将该文件改为下面的内容:
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics; namespace MyDemos
{
public class Chapter
{
public string ChapterName { get; set; }
public ChItem[] ChapterItems { get; set; }
} public class ChItem
{
public Type type { get; set; }
public string Title { get; set; }
public string Desc { get; set; }
} public class MyExpandableListAdapter : BaseExpandableListAdapter
{
private readonly Context context;
private readonly IList<Chapter> chapter;
public MyExpandableListAdapter(Context context, IList<Chapter> chapter)
{
this.context = context;
this.chapter = chapter;
} public override int GroupCount
{
get { return chapter.Count; }
} public override bool HasStableIds
{
get{ return true; }
} public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
return null;
} public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
} public override int GetChildrenCount(int groupPosition)
{
var ch = chapter[groupPosition];
return ch.ChapterItems.Length;
} public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null)
{
var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
view = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem2, null);
}
var ch = chapter[groupPosition];
var chItem = ch.ChapterItems[childPosition];
TextView textView = view.FindViewById<TextView>(Android.Resource.Id.Text1);
textView.Text = chItem.Title;
textView.SetTextColor(Color.Blue);
textView.TextSize = ;
textView = view.FindViewById<TextView>(Android.Resource.Id.Text2);
textView.Text = chItem.Desc;
textView.TextSize = ;
return view;
} public override Java.Lang.Object GetGroup(int groupPosition)
{
return null;
} public override long GetGroupId(int groupPosition)
{
return groupPosition;
} public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null)
{
var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
view = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem1, null);
}
var ch = chapter[groupPosition];
TextView textView = view.FindViewById<TextView>(Android.Resource.Id.Text1);
textView.Text = ch.ChapterName;
textView.TextSize = ;
return view;
} public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
}
5、修改MainActivity.cs文件
将该文件改为下面的内容:
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Com.Baidu.Mapapi.Model;
using Com.Baidu.Mapapi;
using MyDemos.SrcDemos; namespace MyDemos
{
[Activity(Label = "MyDemos--毛毛雨的博客乐园",
MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private IList<Chapter> chItems;
private MyBroadcastReceiver myReceiver; //自定义的中心点位置经纬度,可以根据需要改为你自己感兴趣的任意经纬度
public static readonly LatLng MyLatLng = new LatLng(34.824635, 114.315745); protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); // 注册广播监听
IntentFilter intentFilter = new IntentFilter();
intentFilter.AddAction(SDKInitializer.SdkBroadtcastActionStringPermissionCheckOk);
intentFilter.AddAction(SDKInitializer.SdkBroadtcastActionStringPermissionCheckError);
intentFilter.AddAction(SDKInitializer.SdkBroadcastActionStringNetworkError);
myReceiver = new MyBroadcastReceiver(this);
RegisterReceiver(myReceiver, intentFilter); CreateChItems();
var adapter = new MyExpandableListAdapter(this, chItems);
var expandableListView1 = FindViewById<ExpandableListView>(Resource.Id.expandableListView1);
expandableListView1.SetAdapter(adapter);
expandableListView1.ChildClick += (sender, args) =>
{
var type = chItems[args.GroupPosition].ChapterItems[args.ChildPosition].type;
if (type != null)
{
StartActivity(new Intent(this, type));
}
};
} protected override void OnResume()
{
base.OnResume();
} protected override void OnDestroy()
{
base.OnDestroy();
// 取消广播监听
UnregisterReceiver(myReceiver);
} private void CreateChItems()
{
chItems = new List<Chapter>();
chItems.Add(new Chapter()
{
ChapterName = "第1章 开发环境安装和配置",
ChapterItems = new ChItem[]
{
new ChItem {type=null, Title="本章无示例", Desc = "" },
}
});
chItems.Add(new Chapter()
{
ChapterName = "第2章 Android应用程序的调试和部署",
ChapterItems = new ChItem[]
{
new ChItem { type=typeof(ch0201Main), Title="例2-1 Phoneword", Desc = "演示如何导航到另一个界面以及如何拨号" },
}
});
//后面章节的内容与前面类似,就不再全部列出了
……
}
}
}
OK,以后再添加后续章节的示例时,只需要在CreateChItems()方法中添加对应的代码即可,而其他内容则不需要做任何改变:
chItems.Add(new Chapter()
{
ChapterName = "……",
ChapterItems = new ChItem[]
{
……
};
});
下一讲接着介绍第8章的内容。