[Android] Chapter 19.0 code optimization in the previous chapter and sample Main Interface in this chapter, android19.0

Source: Internet
Author: User

[Android] Chapter 19.0 code optimization in the previous chapter and sample Main Interface in this chapter, android19.0

Category: C #, Android, VS2015;

Created on: 1. Introduction

This chapter describes the basic usage of shared storage and content providers. 2. Optimize the code in the previous chapter.

In the examples in the previous chapter, IntentFilter is not added for each example. However, when you learn this chapter step by step in the order of chapters, you should be aware of the following problems:

(1) it may be inappropriate to let every example receive notifications of MainActivity, because each example is an independent module and it may have no direct connection with the main interface (MainActivity.

(2) You may want to embed a common part in every page you design, and the "common" part only needs to be designed once, just like a one-time design template in WPF or Web that you want to embed on all pages, Fragment can be used for implementation.

The following describes how to solve these two problems.

1. Solution to problem 1

The solution to the (1) problem is simple, that is, add the following filter attribute declaration to the main Activity of each independent module:

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

For specific implementation, see the example in this chapter.

Among them, ch. MyDemosCategory is a constant defined in the ch. cs file.

Because every constant in the array is a string, the new [] in the code can also be written as new string [].

In addition, save the list created in MainActivity to the newly added ch. cs file:

Using System. collections. generic; namespace MyDemos. srcDemos {public static class ch {// define a completely consistent Intent filter category for the entire project. public const string MyDemosCategory = "cnblogs.com. rainmj. myDemos "; public static IList <Chapter> CreateChItems () {IList <Chapter> chItems = new List <Chapter> (); chItems. add (new Chapter () {ChapterName = "Chapter 1st Development Environment installation and configuration", ChapterItems = new ChItem [] {new ChItem {type = null, title = "this chapter has no examples ", Desc = ""},}});...... (The code below is no longer pasted )}}}

In this way, the code in the MainActivity. cs file looks clean and tidy.

Starting from this chapter, all the Code related to MainActivity. cs in the main interface of the subsequent chapter examples will be added to the ch. cs file.

2. Solution to problem 2

As we have mentioned earlier, the layout element of Android is equivalent to the Fragment element of WPF. In this case, you can also design a page separately, and then treat the page as a Fragment as needed. Just embed it into the page you want to embed.

The code for the MyDemosFragment. axml file is as follows:

<?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="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:textStyle="bold"        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>

The following is the code of the MyDemosFragment. cs file under the root directory of the project. This file is equivalent to the Controller in MVC and is used to add the MyDemosFragment. axml file to the page that references it:

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);        }    }}

After completing the above work, you need to set MyDemosFragment. axml is part of another page, such as ch1901Main. part of the axml file, only need to be in ch1901Main. add the following code to the appropriate location in the axml file:

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

Starting from this chapter, all the following sample pages will be embedded in this section.

In addition, the Main. axml file replaces the relevant part with this code. Iii. Main Interface of this Chapter

1. Run

2. Code related to the examples in this chapter in the ch. cs File

ChItems. add (new Chapter () {ChapterName = "Chapter 19th shared storage and content providers", ChapterItems = new ChItem [] {new ChItem {type = typeof (ch1901MainActivity ), title = "example 19-1 basic usage of SharedPreferences", Desc = "demonstrate basic usage of SharedPreferences"}, new ChItem {type = typeof (ch1902MainActivity ), title = "example 19-2 demonstrate basic actionprovider usage", Desc = "demonstrate basic usage of the shared actionprovider"}, new ChItem {type = typeof (ch1903MainActivity ), title = "example 19-3 basic ContentProvider usage", Desc = "demonstrate the basic usage of the content provider and its related classes "},}});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.