Usage of toast in Android

Source: Internet
Author: User

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. Example

Button button1 = (button) Findviewbyid (r.id.button_1);
Button1.setonclicklistener (New View.onclicklistener ()
{
public void OnClick (View v) {
Toast.maketext (Firstactivity.this, "You clicked Button 1", Toast.length_short). Show ();
}
}
);

Usage of toast in Android

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.