1) What is activity
Activity is the easiest place to attract users, it's a component that can contain a user interface,
Primarily used to interact with users.
2) Create the activity manually
1. New package in SRC, then new class inherits from activity, then click Finish.
2. For the activity, we will rewrite its oncreate () method. We can add what we want in this method as needed.
3. Create and load layouts, build a new Android XML file in the Res/layout directory, and then edit it in XML files, which can be written in the desired form.
< . .. android:id = "@+id/button" ... >
Here @+ means defining an ID with a value of button so that we can get it in the class with R.
Public class extends Activity { @Override protectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (R.layout.button); }}
In this code, Setcontentview means loading the layout, passing in the ID of a layout file, and here is the ID we just defined.
4. Registration of the activity, all the action must be registered in Androidmanifest.xml to take effect.
<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.activitytest"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Firstactivity "Android:label= "This is firstactivity" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
The declaration of the newly registered activity is placed between the application tags, where the activity is registered through the Activity tab. In the above code Activity tab, name formulates the specific activity, which is written here. Firstactivity, because the package attribute is already specified in the outermost manifest tag, so just write the name of the current activity. The label represents the contents of the title bar. In the intent-filter tab We have added action android:name= "Android.intent.action.MAIN"/ and category Android: Name= "Android.intent.category.LAUNCHER"/, which means that this is the main activity, that is, click on the Desktop app to open this program first.
5.toast,toast is a reminder that you can use in your program to notify users of short information. This information disappears automatically after a period of time and does not occupy any screen space.
First define the trigger point, which can be a button or something. Add code in OnCreate:
protected void OnCreate (Bundle savedinstancestate) { super .oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.first_layout); Button button1 = new Onclicklistener () {@Override public void OnClick (View v) {toast.maketext (firstactivity. this , "You clicked Button 1" toast.length_s Hort). Show (); } });}
In the event, the Findviewbyid () method can be used to get to the element defined in the layout file, where we pass in r.id.button_1 to get an instance of the button, which was specified in the First_layout.xml by the Android:id attribute just now. The method obtains a View object, which we need to force into the button object. Then we call the Setonclicklistener () method to register a listener for the button, and the OnClick () method in the listener executes when the button is clicked. Therefore, the ability to eject a Toast is, of course, written 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. The Maketext () method requires the passing of three parameters. The first parameter is the context, which is the content of the toast request, the second argument is the text contents of the toast display, the third parameter is the length of the toast display, and two built-in constants can be selected Toast.length_short and Toast.length_ LONG.
6. Destroying an activity, the activity class provides a finish () method, which we call this method in the activity to destroy the current activity.
Button1.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (View v) { finish (); }});
Rerun the program, click the button, the current activity has been successfully destroyed, the effect and press the back key
is the same.
Here to learn the basic usage of activities.
Android Learning Activities (activity)