Android Learning Note 5: Explore activity 2

Source: Internet
Author: User

The first line of code--android Guo Lin

Using toast in an activity

Toasts are a great way to alert your Android system that you can use it to notify users of short messages that will disappear after a certain period of time and will not occupy any screen space, so let's try to use toasts in our activities now.

The method of using toast is very simple, add code in the OnCreate () method:

Button button1 = (Button) findViewById(R.id.button_1);button1.setOnClickListener(new OnClickListener() {    @Override    publicvoidonClick(View v) {        Toast.makeText(FirstActivity.this"You clicked Button 1",        Toast.LENGTH_SHORT).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 Findviewbyid () method returns a View object, which we need to transform downward to turn it into a button object. After getting an instance of the button, we register a listener for the button by calling the Setonclicklistener () method, and the OnClick () method in the listener is executed 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. It is important to note that the Maketext () method needs to pass in three parameters. The first parameter is the context, which is a request for a toast, because the activity itself is a contextual object, so it can be passed directly to Firstactivity.this. The second parameter is the text content of the toast display, the third parameter is the length of the toast display, and there are two built-in constants to choose Toast.length_short and Toast.length_long.

The effect is as follows:

Using the menu in an activity

Menus are often used in mobile phone applications. Usually the menu is hidden, only when the menu key is pressed, the menu pops up, we choose the function we need. How to achieve this effect? The method is to use menu in the activity.

    • First we add the menu folder under Res, just as you would add a layout folder under Res in the previous article. Such as.

    • On the Menu folder, right-click New→menu resource file, as shown in, fill in the filename as main. Click OK to generate the Main.xml file.

    • Open the Main.xml file, and insert the following code:

<item    android:id="@+id/add_item"    android:title="Add"/><item    android:id="@+id/remove_item"    android:title="Remove"/>

Here we create two menu items, where the tag is used to create a specific menu item, and then assign a unique identifier to the menu item by Android:id, specifying a name for the menu item by Android:title.
* Then open firstactivity, overriding the Oncreateoptionsmenu () method, as shown in the code below:

publicbooleanonCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main, menu);    returntrue;}

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, and the first parameter specifies which resource file we use to create the menu. Here, of course, the R.menu.main is passed in, and the second parameter is used to specify which menu object our item will be added to, directly using the menu parameter passed in the Oncreateoptionsmenu () method. This method is then returned to true to indicate that the menu created is allowed to be displayed, and if False is returned, the created menu will not be displayed.

    • To make our menu not only display, but also respond to the action, let's add a response function to it. Open firstactivity, overriding the Onoptionsitemselected () method:
public  boolean  onoptionsitemselected  (MenuItem item) {switch  (Item.getitemid ()) {case  R.id.add_item:toast.mak EText (this ,  "you clicked Add" , Toast.length_        Short). Show ();    break ; case  R.id.remove_item:toast.maketext (this ,  "you clicked Re        Move ", Toast.length_short). Show ();    break ; default :} return   True ;}  

In the Onoptionsitemselected () method, by calling Item.getitemid () to determine which menu item we clicked on, and then adding our own logic to each menu item, here we are ingenious to pop a toast that we just learned.
* Run the program: Hey.. Where is the menu key??? To better demonstrate the effect, let's change the firstactivity inherited from activity to inherit from Appcompatactivity. Now the title bar appears, let us click on the top right corner of the small icon, the menu is displayed, the effect is as follows.

Destroy an activity

How do I destroy an activity?

In fact, the answer is very simple, just click the Back button to destroy the current activity. But if you don't want to press the button, but you want to destroy the activity through code in the program, of course, the activity class provides a finish () method, which we can use to destroy the current activity by invoking this method in the activity.
Modify the code in the button listener as follows:

button1.setOnClickListener(new OnClickListener() {    @Override    publicvoidonClick(View v) {        finish();    }});

Re-run the program, click the button, the current activity has been successfully destroyed, the effect and press the back key is the same.

Android Learning Note 5: Explore activity 2

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.