I. Create a project
- 1
Project Name: myactivity
Package Name: com.iflytek.myactivity
- 2
To facilitate learning, do not tick create Activity. Then finish, the project is created
END
Two. Create an event
- 1
Create a package. The package is named: Com.iflytek.myactivity.
Create method: Right-click src directory->new->package, fill in the package name can
- 2
Create a class that is named Activityfirst.
Create method: Right-click the package->new->class just now.
- 3
Because you want to create an activity, you must inherit from the activity base class with the following code:
- 4
Create an activity, code, explained below:
We have added the following method OnCreate in Activityfirst. Based on the activity's life cycle, when an activity is initialized, a oncreate is first invoked to create an activity. So, we added the OnCreate method.
Bundle: This type is similar to the map type and stores the data in a key-value manner;
Savedinstancestate: It is useful to save the state of an activity, which is called before an activity is completed. For example, using a reader to read a novel, close the reader, the next time you open, will still stay in the last place to see.
Super.oncreate (): Super is a property or method that calls the parent class, here is the OnCreate method that first runs the parent class
For more study, please scan QR code attention public Number: It_eclassroom
Three. Creating and loading layouts
- 1
The Android program is designed with logic and view separation. I just implemented the logic in Java code and created the activity, but there's nothing in this activity. So, you need to make a layout for him.
- 2
->new->android the XML file in the Res/layout directory to create a layout file. The command is Layout_first.
When the creation is complete, select Layout_first.xml to switch to edit mode. Add the following in your code:
<button
Android:id= "@+id/button_1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Button 1"
/>
Explained below:
1) <button/>: This is a button control, we add a button in the current layout file;
2) Android:id: Unique identifier for this button. Can be referenced in code; "@+id/button_1" means to add a unique ID to the button named Button_1
3) Android:layout_width:layout is the layout meaning, Layout_width is the width of the control. and is relative to the width of its parent element. Only match_parent (full space), wrap_parent (just to show exactly what you want to show)
Android:text: What to display
- 3
The layout is complete and the layout is added to the code below
Back to the previous code, add the following line:
Setcontentview (R.layout.layout_first);
This means: The current activity uses the Layout_first layout file for layout.
R: is a class, layout is an inner class of R, and Layout_first is a class variable in this inner class.
END
Four. Registering in the Androidmanifest file
- &NBSP;
All activities need to be registered in this file, add code as follows
<activity
android:name= "Com.iflytek.myactivity.ActivityFirst"
android:label= "This is firstactivity"
< Intent-filter>
<action android:name= " Android.intent.action.MAIN "/>
<categor Y android:name= "Android.intent.category.LAUNCHER"/>
& Lt;/intent-filter>
</activity>
Android:name The name of the activity is the package name + class name, because you need to tell the file which class you register, he can find this activity;
Android:label acvitity tags, after the software opens, This activity's title bar displays the name.
Intent-filter: Intent filter, not much to say here, in short, with a few words, it means to use this actitity as our main activity of this program
- 2
OK, right click on the myactivity, choose Run as->android application, get it running
Android Development--the first event