Android learning 2 (Activity)

Source: Internet
Author: User

Android learning 2 (Activity)

Previously, I briefly introduced some basic knowledge about android. as a warm-up, I will be able to access the activity activities of four android components.

1. Activity is a component that protects the user interface and is mainly used to interact with users.

 

Basic activity usage

1. Create a project named ActivityTest, Which is manually created.

Select file-> android project and fill in next for the project name and package name. Do not check craete activity and finish

2. Create a good package, create a class FirstActivity, and inherit the Activity

3. Rewrite the onCreate method. The Code is as follows:

 

@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}
4. Create and load a layout

 

Right-click the res/layou directory new-> android xml file, select first_layout as the layout file name, and select linear layout.

5. Return to FirstActivity and set the layout for the activity. The Code is as follows:

 

package com.wj.test;import android.app.Activity;import android.os.Bundle;public class FirstActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.first_layout);}}

 

Note that the R here is the R under the recently created package. The android sdk will automatically provide an R file under the android package. Do not make a mistake.

 

6. registration activities

 

     
                          
                                   
                
    
               
  
 

 

7. Run your android Project

 

 

The running result is as follows:

 

Android: label = this is firstactivity specifies the title bar of the activity. The title bar is the content displayed at the top of the activity.

 

7. Hide the title bar

Use requestWindowFeature (Window. FEATURE_NO_TITLE); this sentence hides the title bar. Note that this must be used before setContentView (R. layout. first_layout. RequestWindowFeature (Window. FEATURE_NO_TITLE) means that the title bar is not displayed in the activity.

 

8. Use Toast during the activity

Add the following code to onCreat:

Button button1=(Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(FirstActivity.this, you clicked button1,Toast.LENGTH_SHORT).show();;}});

 

 

 

Button button1 = (Button) findViewById (R. id. button1); get the elements defined in the layout. A view object is returned.

Create a Toast object using the static method makeText () of Toast, and call show to display Toast. MakeText () requires three parameters. The first parameter is Context, that is, Context,

The second parameter is the text content, and the third parameter is the display duration.

 

 

9. Use menu in the activity

First, create a menu folder under the res directory, right-click the directory-> new-> folder, enter the File menu, and click finish. Then, create a menu file named main under this folder, right-click the menu folder --> new --> android xml file. The main. xm code is as follows:

 

 

Override the onCreateOptionsMenu (Menu menu) Method

 

 

@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// TODO Auto-generated method stubgetMenuInflater (). inflate (R. menu. main, menu);/*** the getMenuInflater () method can obtain the MenuInflater object. After calling its inflate method, you can 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. Here, of course, input the R. menu. main, the second parameter is used to specify the Menu object to which the Menu will be added. Here, the * menu parameter passed in using onCreateOptionsMenu (menu Menu) is used directly. Then return true to this method, table. Shows * the menus that can be created are displayed. If false is returned, the created menus cannot be displayed. **/return true ;}

 

 

To respond to the menu, we also need to override the onOptionsItemSelected (MenuItem item) method. The Code is as follows:

 

@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stub/** call item. getItemId () to determine which menu item we click. **/Switch (item. getItemId () {case R. id. add_item: Toast. makeText (FirstActivity. this, you clicked add, Toast. LENGTH_SHORT ). show (); break; case R. id. remove_item: Toast. makeText (FirstActivity. this, you clicked add, Toast. LENGTH_SHORT ). show (); break; default: break;} // return super. onOptionsItemSelected (item); return true ;}

 

Running result:

 

 

 

10. Destroy an activity and call the finish () method. For example, call the finish () method in the listener function of button1 to disable the activity.

 

Let's talk about activity first. Next, I will introduce Intent.

Reprinted Please note: http://blog.csdn.net/j903829182/article/details/40479181

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.