This series is the "first line of Android Code" Learning notes, if there are errors and omissions, please correct me!
Activity is the easiest place to attract users, a component that can contain a user interface, primarily for interacting with users. An application can contain 0 or more activities. Activity is one of the four major Android components, let's explore an activity below.
1. Create an event:
We can create an activity according to the process in the study note (a), and then we can see the default OnCreate () method that the IDE has written for us:
The default OnCreate () method is very simple, which is to call the OnCreate () method of the parent class. In a project, each activity has to rewrite the activity's OnCreate () method according to its own needs, and we will slowly join in the various logic implementations later.
Java tip:
1) Super: In the Java class we use the Super keyword to refer to the components in the base class;
2) @override: This keyword can detect whether the name of the method differs from the base class, and if it is different, it will be an error. If this keyword is not added, the system treats it as a new method when the name differs from the base class.
2. Build the XML layout file:
The Android program is designed to be logical and view-separated, and it's best to have one layout for each activity, and the layout is to display the interface content, so we'll build a layout file now.
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/button_1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Button 1" /></LinearLayout>
You can see that this layout file selects LinearLayout as the root directory and then adds a button element to it:
1)Android:id= "@+id/button_1" : This statement defines a unique identifier for the current element, which can then be manipulated in code;
2)android:layout_height= "Wrap_content" : This statement specifies the height of the current element, where wrap_content is used, Indicates the height of the current element as long as it can contain exactly what is inside;
3)Android:layout_width= "Match_parent" : Specifies the width of the current element, which uses match_parent to make the current element as wide as the parent element;
4)Android:text= "button 1" : Specifies the text content displayed in the element.
If you re-created an activity yourself, remember to add the Setcontentview () method to the activity to load the layout file and register the activity in the manifest file after the layout element has been built.
No accident, run the program after the interface as follows:
3. Hide the title bar:
If we want to hide the title bar of the active interface, it is easier to add this statement to the activity: requestwindowfeature (Window.feature_no_title);
Note: This code must be executed before Setcontentview (), otherwise it will be an error.
4. Use toast in the event:
Toast is a great way for an Android to alert you to a message that pops up at the bottom of the screen, disappears automatically after a while, and does not occupy any screen space. Now that we have a BUTTON element in our program, we can use it as a toast trigger point, the code is as follows:
Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Button button1=(Button) Findviewbyid (r.id.button_1); Button1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "You clicked Button 1", Toast.length_short). Show (); } }); }}
First, we need to get an instance of the button to manipulate it, where we get to the element defined in the layout file by the Findviewbyid () method, where we pass in r.id.button_1 to get the instance of the button, which was just in First_ specified by the Android:id property in the Layout.xml. The Findviewbyid () method returns a View object, which we need to transform downward to turn it into a button object.
Second, we register a listener for the button by calling the Setonclicklistener () method, and the OnClick () method in the listener executes when the button is clicked.
Next, write the Toast method in the OnClick () method. A toast is very simple to use, creating a Toast object with a static method Maketext () and then calling Show () to display the toast. where Maketext () passes three parameters:
1) The first parameter is the context, that is, a toast requirement, which is passed directly to mainactivity. this can be;
2) The second parameter is the text content of the toast display;
3) The third parameter is the length of the Toast display, with two built-in constants that can be selected Toast.length_short and Toast.length_long.
After you run the program, when you click Button 1, a message pops up at the bottom of the screen:
5. Use the menu element in the activity:
1) Create and write a menu layout file:
(1) First in the Res folder right → select new→ Select android Resource Directory, the following interface appears, fill in the name, click OK:
(2) There will be a menu folder under the Res folder:
On top of it, right-click →new→ Select Menu Resource file.
(3) write the layout Code of the menu:
<?XML version= "1.0" encoding= "Utf-8"?><Menuxmlns:android= "Http://schemas.android.com/apk/res/android"> <ItemAndroid:id= "@+id/add_item"Android:title= "Add"/> <ItemAndroid:id= "@+id/remove_item"Android:title= "Remove"/></Menu>
Two item is declared in the file and will appear on the menu.
2) The logical implementation of the menu:
We rewrite Oncreateoptionsmenu() and onoptionsitemselected() in the activity, with the following code:
@Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.add_item:toast.maketext ( This, "You clicked Add", Toast.length_short). Show (); Break; CaseR.id.remove_item:toast.maketext ( This, "You clicked Remove", Toast.length_short). Show (); Break; default: } return true; }
(1) The Getmenuinflater () method can be used to get the Menuinflater object, and then call its inflate () method to create a menu for the current activity. The inflate () method receives two parameters, the first parameter is used to specify which resource file we use to create the menu, which of course passes in R.menu.main, and the second parameter specifies which menu item will be added to it, directly using the The menu parameter that is passed in the Oncreateoptionsmenu () method.
(2) in the Onoptionsitemselected () method, by calling Item.getitemid () to determine which menu item we clicked on, and then adding our own logic processing to each menu item.
Effect after program run
6. Destroy an activity:
The activity class provides a finish () method that we can destroy the current activity by invoking this method in the activity. Such as:
//Modify the code in the button listener as follows: Button1.setonclicklistener (new Onclicklistener () {@Override publicvoid OnClick (View v) {finish ();}});
End.
Android Learning Notes (ii)--Explore an activity