Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 【Android】MyDemos項目的結構和主界面相關代碼,androidmydemos

【Android】MyDemos項目的結構和主界面相關代碼,androidmydemos

編輯:關於android開發

【Android】MyDemos項目的結構和主界面相關代碼,androidmydemos


分類: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:text
        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:text
        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 = 12;
            textView = view.FindViewById<TextView>(Android.Resource.Id.Text2);
            textView.Text = chItem.Desc;
            textView.TextSize = 9;
            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 = 14;
            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章的內容。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved