Method analysis of creating an activity for Android

This article gives an example of how Android creates an activity. Share with you for your reference, as follows:

In order to create a new activity, you need to inherit the activity class, define the UI and implement the functions. The basic framework code of the new activity is as follows:

The basic activity class represents an empty screen, which is of little use, so the first thing is that you fill the UI with views and layouts.

The UI of activity is created by views. Views are UI controls that display data and provide user interaction. Android provides some layout classes, called view groups, which can accommodate multiple views to help you design complex UI.

Previously, we talked about view and view groups, involving the use and creation of custom views and layouts.

Specify UI for Activity, please call the setContentView method in the onCreate method of Activity.

In this code fragment, a simple instance of MyView is used as the UI of the activity:

In most cases, you want to use more complex UI design. You can use view groups in the code to create a layout, or you can use the convenience of standard Android to pass the resource ID of an externally defined layout, as shown in the following code fragment:

In order to use an activity in your application, you need to register it in the manifest. Add a new activity tag in the application node; The activity contains metadata such as label, icon, permissions and themes. An activity without a corresponding activity tag cannot be started.

The following XML fragment shows how to add a node to the myactivity class I just created:

In the activity tab, you can add an intent filter node to indicate which intention your activity listens to and responds to. Each intent filter can define one or more actions and categories. Intent and intent filter will be described in detail in Chapter 5, but it is of little value to an activity as the main initiator. It must include an intent filter to listen to the main action and launcher type, as shown in the following high brightness code fragment:

For more Android related content, interested readers can view the special topics of this site: activity operation skills summary of Android programming, Android communication mode summary, Android debugging skills and common problem solving methods summary, introduction and advanced teaching of Android development, usage summary of Android basic components, Android layout skills summary, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>