First Android Activity program from scratch

Source: Internet
Author: User

I. class hierarchy:
 
2. What is Activity and how to understand Activity
1. interfaces for user interaction with Applications
2. Control container. We need to place the control in this container.
 
 
3. How to create an Activity
Create a class:
1. inherit the Activity class

Package com. jiahui. activity;
 

 
Import android. app. Activity;
 
Import android. OS. Bundle;
 

 
Public class MyActivity01Activity extends Activity {
 
/** Called when the activity is first created .*/
 
@ Override
 
Public void onCreate (Bundle savedInstanceState ){
 
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. main );
 
}
 
}

2. Override the onCreate () method. This method is called when the Activity runs for the first time. The caller of this method is called by the application framework system.
3. Register in AndroidMainfest. xml (for AndroidMainfest. xml)
 

<Activity android: name = ". MyActivity01Activity"
 
Android: label = "@ string/app_name">
 
<Intent-filter>
 
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
 
</Intent-filter>
 
</Activity>

This Activity is created.
To make the display look good, we can add some necessary controls for the Activity. Here we add a TextView
All the controls in main. xml under the Layout file must be registered here.

<? 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 = "fill_parent">
 
<TextView android: id = "@ + id/myText" android: layout_width = "fill_parent"
 
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
 
<Button android: id = "@ + id/myButton" android: layout_width = "fill_parent"
 
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
 

Back to the previous steps in the onCreate Method
You can use the findViewById () method to obtain each control. This method returns a View object, so we need to convert it down to the desired control class.
 

Package com. jiahui. activity;
 

 
Import android. app. Activity;
 
Import android. OS. Bundle;
 
Import android. widget. Button;
 
Import android. widget. TextView;
 

 
Public class MyActivity01Activity extends Activity {
 

 
Public void onCreate (Bundle savedInstanceState ){
 

 
Super. onCreate (savedInstanceState );
 

 
SetContentView (R. layout. main); // sets the layout file used by Activtiy.
 

 
TextView myTextView = (TextView) findViewById (R. id. myText );
 
Button myButton = (Button) findViewById (R. id. myButton );
 

 
MyTextView. setText ("My first TextView ");
 
MyButton. setText ("My first button ");
 

 
}
 
}
 

Running result:

From: jiahui524 Column

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.