Android to create an activity method analysis _android

Source: Internet
Author: User

The examples in this article describe how Android creates an activity. Share to everyone for your reference, specific as follows:

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

Package com.paad.myapplication;
Import android.app.Activity;
Import Android.os.Bundle;
The public class MyActivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '
./
@Override public
void OnCreate (Bundle icicle) {
super.oncreate (icicle);
}
}

The basic activity class represents an empty screen, it's not much use, so the first thing is that you populate the UI with views and layouts.

The UI for an activity is created by views. Views are UI controls that display data and provide user interaction. Android offers a number of layout classes, called View Groups, that can hold multiple view to help you design complex UIs.

The view and view Groups are described earlier, involving the use and creation of custom views and layouts.

To specify the UI for the activity, call the Setcontentview method in the OnCreate method of the activity.

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

@Override public
void OnCreate (Bundle icicle) {
super.oncreate (icicle);
MyView myview = new MyView (this);
Setcontentview (MyView);
}

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

@Override public
void OnCreate (Bundle icicle) {
super.oncreate (icicle);
Setcontentview (R.layout.main);
}

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

The next XML fragment shows how to add a node to the MyActivity class I just created:

<activity android:label= "@string/app_name"
android:name= ". MyActivity ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity >

In the Activity tab, you can add Intent-filter nodes to indicate which intent your activity listens to and responds to. Each intent filter can define one or more action and categories. Intent and Intent filter are described in detail in the 5th chapter, but it is of little value to an activity as a primary starter program. It must contain a intent filter to listen for the main action and the launcher kind, as shown in the following high brightness code fragment:

<activity android:label= "@string/app_name"
android:name= ". MyActivity ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</ Activity>

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Communication method Summary", "Android debugging techniques and common problems solution summary", " Android Introduction and advanced tutorials, summary of Android Basic components usage, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Related Article

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.