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