Android first line of code learning Note two---using toast in an activity

Source: Internet
Author: User

Toast: A great way to remind your Android system that it can be used to notify users of short messages that will automatically disappear after a certain period of time and will not occupy any screen space.

First, you need to define a popup toast trigger point, and then note a program, just above a button, we click on this button when a toast pops up, in the OnCreate () method, add the following code:

    protected void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (r.layout.firstlayout);         = (Button) Findviewbyid (r.id.button1);        Button.setonclicklistener (new  View.onclicklistener () {            public  void  OnClick (view view) {                Toast.maketext (firstactivity. This, "You Clicked Button1", Toast.length_short). Show ();}}        );     }}

In the event, you can get the elements of the layout file definition through the Findviewbyid () method, where we pass in R.id.button1 and get an instance of the button, which we specified in Firstlayout by the Android:id attribute. After we get an instance of the button, we register a listener for the button by calling the Setclicklistener () method, 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 Onclik () method.

The usage of toast is very simple, create a Toast object by static method Maketext (), and then call Show () to display the toast (), here Maketext () passed three parameters, the first parameter is the context, That is, the context of the toast request, because the activity itself is a context object, so it is directly passed into the firstactivity.this. The second parameter is the text content of the toast display, and the third parameter is the length of the toast display, with two built-in constants Toast.length_short and Toast.length_long.

Android first line of code learning Note two---using toast in an activity

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.