program function: Click on a button and then pop up a hint message
Step 1: Create an empty Android project by creating a new Android Application project in Eclipse, without checking the create activity during creation.
At this point, both SRC and res/layout are empty.
Step 2: Create a new Activity in Src to inherit from android.app.Activity, click Finish to generate the code as follows:
1 Package Cn.csc.hello_world; 2 3 4 5 Import android.app.Activity; 6 7 8 9 Public class extends Activity {ten A -}
Press Shift+alt+s, select Override/implement Methods in the pop-up menu:
Tick OnCreate (bundle) and click OK to implement the OnCreate (bundle) method of the activity. The automatically generated code is as follows:
1 @Override 2 3 protected void onCreate (Bundle savedinstancestate) {45 // TODO auto-generated Method Stub 6 7 Super . OnCreate (savedinstancestate); 8 9 }
Step 3: Create a new layout file with only one button in the Res/layout directory
Right-click Layout-"New"-Android Xml File:
File name is First_layout
Root node Selection LinearLayout
Then click Finish to complete
The auto-generated code in First_layout.xml is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 3 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"4 5 Android:layout_width= "Match_parent"Width: Fills the parent node6 7 Android:layout_height= "Match_parent"Height: Populate parent node8 9 android:orientation= "vertical" >Sub-node arrangement direction: vertical arrangementTen One </LinearLayout>
Add a button child node in the LinearLayout, enter Butt, and then alt+/, you can automatically complete the button child node, to add properties, you may also enter a few initials, and then alt+/complement
1 <Button2 3 Android:id= "@+id/btn"add an ID that can be referenced later when this button is r.id.btn4 5 Android:layout_width= "Wrap_content"Package Contents can be6 7 Android:layout_height= "Wrap_content"Package Contents can be8 9 Android:text= "I ' m a button"the text value displayed on the buttonTen One />
Here, the Text property is set directly to a string, hard coding is not good, mouse points to "I m a button", so that the cursor stays anywhere in the string, and then press CTRL + 1 (is the number 1), in the popup menu, select Extract Android String:
Replace by R.string.btntext, and then click the Ok,text property to: android:text= "@string/btntext", to refer to this string later, you can use R.string.btntext.
Step 4: Set the layout file for the newly created firstactivity, First_layout.xml
In the Firstactivity OnCreate () method, add the above:
1 protected void onCreate (Bundle savedinstancestate) {23 // TODO auto-generated Method Stub 4 5 Super . OnCreate (savedinstancestate); 6 7 Setcontentview (r.layout.first_layout); 8 9 }
At this point, you will find R.layout. First_layout,setcontentview () Statement error
Observing the Firstactivity code, you will find that there is an extra line of import Android. R
Delete it, or an error, indicating that R is not found.
(Note that if you press CTRL + 1 for code repair, import Com.example.hello_world. R
Although no further error is made, the full package name is not specified when the activity is registered later. The class name can be a problem that the program cannot run, and an error message indicates that the required activity could not be found. For the sake of simplicity, the following modifications can be made to the package property operation, of course, you can also do not modify, after the registration of each component, pay attention to pack name.
Locate the Android Manifest.xml file and see the package property in the root node Manifest: Not the Firstactivity.java package name that we set up, but the one that is the default when the project was created, and the package Cn.csc.hello_ The world was built after that. This results in R.java not being in the same package as Firstactivity.java and therefore cannot be accessed directly.
1 < xmlns:android= "Http://schemas.android.com/apk/res/android"23 Package = "Com.example.hello_world" 4 5 android:versioncode= "1"67android:versionname = "1.0">
Modify the Package property value to Firstactivity's name: package= "Cn.csc.hello_world".
This way R.java and Firstactivity.java are in the same package and can be accessed directly.
Then go back to the firstactivity code and find that it's no longer an error.
Step 5: Find the added button and set its click Response event
The OnCreate () code is modified as follows:
1 protected voidonCreate (Bundle savedinstancestate) {2 3 //TODO auto-generated Method Stub4 5 Super. OnCreate (savedinstancestate);6 7 Setcontentview (r.layout.first_layout);8 9Button BTN =(Button) Findviewbyid (R.ID.BTN);Ten OneBtn.setonclicklistener (NewOnclicklistener () { A - @Override - the Public voidOnClick (View arg0) { - -Toast.maketext (firstactivity. This, "I was clicked", Toast.length_short). Show (); - + } - + }); A at}
Findviewbyid () Find the desired view by ID
Btn.setonclicklistener () Sets the Click event Listener, where an anonymous inner class implementation is selected.
Toast.maketext (). Show (); pop up a hint message
Step 6: Register the firstactivity to be used
After you complete step 5, if you run the program, you will see no effect, because you are not registering the activity you want to use in Android Manifest.xml. So firstactivity will not be used and of course its oncreate () method will not be called.
The registration Firstactivity method is as follows:
To add an activity child node to the application node:
1 <ActivityAndroid:name=". Firstactivity "Android:label= "Hello World" >2 3 <Intent-filter>4 5 <ActionAndroid:name= "Android.intent.action.MAIN"/>6 7 <categoryAndroid:name= "Android.intent.category.LAUNCHER"/>8 9 </Intent-filter>Ten One </Activity>
The Name property must be set to specify which activity class is registered, abbreviated here. Firstactivity, the Package property in the manifest node will be stitched together into the full class name, or the full class name, such as: Cn.csc.hello_world, can be written. Firstactivity
Label property, set the title of the app, or, if not set, the name of the item.
Intent-filter sets the intention to start the activity, where the activity is set to start activity.
At this point, the change program can be run smoothly.
If you do not want to display the application title bar, you can add Requestwindowfeature (window.feature_no_title) in the OnCreate () method;
However, it is important to note that you must add the code before Setcontentview (), or you will get an error.
Summarize:
If this error is not up to r, you can prioritize whether the package name is inconsistent before and after, causing R.java to be in the same package as the code you wrote.
The package property in Manifest.xml is set in the new project and is used to uniquely identify the current application in the phone, the source code packages can be manually created a new package name, if inconsistent with the manifest.xml, you should be aware that when registering each component, you should use the full class qualified name, that is, the package name. Class name. Otherwise, the package property, with the Name property of the registered component, is stitched into the wrong class-qualified name, causing the component's definition class to be missing.
Android Rookie Learning Note 5----first Android program