Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

【Android】第19章 前面章節的程式碼優化及本章示例主界面

編輯:關於 Android 開發

分類:C#、Android、VS2015
建立日期:2016-03-05

一、簡介

本章主題是「共享儲存與內容提供程序」(Shared Storage and Content Providers),會介紹 SharedPreferences、ShareActionProvider 與 ContentProvider 這三組 API 的基本用法。

二、先整理一下前面章節的程式碼

前面章節的示例,並沒有為每個範例都加上 IntentFilter。但當你照著章節順序一路學到這裡,應該已經察覺兩個問題。

問題一:讓每個範例都接收 MainActivity 的通知其實並不恰當。每個範例都是獨立模組,跟主界面之間未必有直接關聯,硬綁在一起反而讓依賴變得醜陋。

問題二:你可能會想,能不能設計一個所有頁面共用的「共同區塊」,只寫一次、到處嵌入?這就像 WPF 或 Web 開發中,我們希望把某段一次性設計好的樣板套用到每一頁一樣。

下面分別說明這兩個問題怎麼解。

1、問題一的解法

做法很簡單:在每個獨立模組的主 Activity 上補一個過濾器特性宣告即可:

[IntentFilter(new[] { Android.Content.Intent.ActionMain }, Categories = new[] { ch.MyDemosCategory })]

其中 ch.MyDemosCategory 是在 ch.cs 中定義的常數。由於陣列中的每個元素都是字串,所以 new[] 也可以明確寫成 new string[]。

接著,把原本散在 MainActivity 裡的章節列表也集中搬到這個新檔:

using System.Collections.Generic;

namespace MyDemos.SrcDemos
{
    public static class ch
    {
        // 為整個專案定義一個完全一致的 Intent 過濾器分類
        public const string MyDemosCategory = "cnblogs.com.rainmj.MyDemos";

        public static IList<Chapter> CreateChItems()
        {
            IList<Chapter> chItems = new List<Chapter>();

            chItems.Add(new Chapter() {
                ChapterName = "第1章 開發環境安裝和配置",
                ChapterItems = new ChItem[] {
                    new ChItem { type = null, Title = "本章無示例", Desc = "" },
                }
            });
            // ……後續章節程式碼從略
            return chItems;
        }
    }
}

這樣一來,MainActivity.cs 就顯得乾淨清爽多了。從本章開始,後續所有章節的示例主界面,凡是與 MainActivity 相關的程式碼,都改到 ch.cs 中維護。

2、問題二的解法

前面提過,Android 的 Fragment 相當於 WPF 中的 Frame 元素。既然如此,我們也可以單獨設計一個頁面,需要時把它當成 Fragment 嵌入到目標頁面中。

以下是 MyDemosFragment.axml 的程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:text="www.cnblogs.com/rainmj/"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:textColor="#FF267F00"
        android:gravity="center_horizontal"
        android:paddingBottom="5dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="#0000FF"
        android:layout_marginBottom="5dp" />
</LinearLayout>

以下是專案根目錄下 MyDemosFragment.cs 的程式碼。這個檔案的角色近似 MVC 中的控制器,負責把 axml 載入並加入引用它的頁面:

using Android.App;
using Android.OS;
using Android.Views;

namespace MyDemos
{
    public class MyDemosFragment : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.MyDemosFragment, container, true);
        }
    }
}

完成上述工作後,若要把 MyDemosFragment.axml 當成另一個頁面的一部分(例如 ch1901Main.axml),只要在合適的位置加上這一段即可:

<fragment
    android:name="MyDemos.MyDemosFragment"
    android:tag="mydemos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

從本章開始,後續所有示例頁面都會嵌入這個區塊,Main.axml 的相關部分也已替換為這段程式碼。

三、本章示例主界面

1、運行截圖

2、ch.cs 中與本章示例相關的程式碼

chItems.Add(new Chapter() {
    ChapterName = "第19章 共享存儲和內容提供程序",
    ChapterItems = new ChItem[] {
        new ChItem {
            type = typeof(ch1901MainActivity),
            Title = "例19-1 SharedPreferences基本用法",
            Desc = "演示SharedPreferences的基本用法"
        },
        new ChItem {
            type = typeof(ch1902MainActivity),
            Title = "例19-2 ShareActionProvider基本用法",
            Desc = "演示共享操作提供程序(ShareActionProvider)的基本用法"
        },
        new ChItem {
            type = typeof(ch1903MainActivity),
            Title = "例19-3 ContentProvider基本用法",
            Desc = "演示內容提供程序(ContentProvider)及其相關類的基本用法"
        },
    }
});
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved