1. Get started with Activity and get started with activity

Source: Internet
Author: User

1. Get started with Activity and get started with activity

Activity is a basic component of Android and an interactive program portal. An Android project consists of multiple activities. All display components must be placed on the Activity for display.

(1) Android project workspace Structure

 

 

As shown in, an Android project workspace consists of several parts. The src directory stores the Activity file, and the gen directory stores the R file, R. java stores all resource IDs of the project. The res directory stores images, webpages, text, and other resources. The drawable directory stores images. It has three resolutions, layout stores xml layout files and is mainly used to arrange different display components. values stores some resource information, such as defining array data, color data, scale data, strings, and styles, raw stores some native files, such as videos and audios. You can use Resources. the getRawResource () method obtains these resources; AndroidManifest. xml is the main configuration file of Android, used to configure various components or some access permissions.

(2) Simple Android Program

An Android project has three main aspects: Layout file, Activity, and resource file. First, write the component layout in the layout file, and then use Activity to call the layout file for display, activity also performs some human-computer interaction operations. Both layout files and Activity can call resource files. The following is a simple Android program example:

First, create an activity_main.xml layout file with only two text boxes and one button in the layout. The Code is as follows:

<LinearLayout xmlns: android =Http://schemas.android.com/apk/res/android"

Android: layout_width ="Match_parent"

Android: layout_height ="Match_parent"

Android: orientation ="Vertical">

 

<TextView

Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"

Android: text ="@ String/hello_world"/>

<TextView

Android: id ="@ + Id/mytext"

Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"/>

<Button

Android: id ="@ + Id/mybutton"

Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"/>

 

</LinearLayout>

Create a MainActivity. java class that inherits the Activity. Call this class to display the layout file and control some components. The Code is as follows:

Public ClassMainActivityExtendsActivity {

 

@ Override

Protected VoidOnCreate (Bundle savedInstanceState ){

Super. OnCreate (savedInstanceState );

SetContentView (R. layout.Activity_main); // Call the layout File

TextView text = (TextView) findViewById (R. id.Mytext); // Obtain the component by id

Button button = (Button) findViewById (R. id.Mybutton);

Text. setText ("id control TextView component"); // you can specify the content of a text box.

Button. setText ("id control Button component"); // sets the button text information

}

}

After writing an Activity class, you must register it in AndroidManifest. xml. Otherwise, an error will be reported. Many people will make this mistake carelessly. Remember it !!! The registration code is as follows:

<Activity

Android: name ="Com. example. chapter3.MainActivity"

Android: label ="@ String/app_name">

<Intent-filter>

<Action android: name ="Android. intent. action. MAIN"/>

 

<Category android: name ="Android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

Action android: name ="Android. intent. action. MAIN"/> Indicates that the Activity is the main class of the project. This is similar to multiple cpp files in the C ++ project, but there is only one program running portal file. The program runs as follows:

 

 

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.