Introduction to Toast of Android control series

Source: Internet
Author: User

Toast is the English meaning of Toast. In Android, it is like the Toast made in the baking machine, and gradually disappears after a short period of time.

Toast is also a container that contains various views and carries them for display.

Use Cases:

1. You need to prompt the user, but you do not need to click "OK" or "cancel.

2. Simple prompts that do not affect the running of existing activities.

Usage:

1. You can initialize through the constructor:

Copy codeThe Code is as follows: // initialize Toast
Toast toast = new Toast (this );
// Set the display time. You can select Toast. LENGTH_LONG or Toast. LENGTH_SHORT.
Toast. setDuration (Toast. LENGTH_LONG );
// Carry a TextView to display text
TextView view = new TextView (this );
// Set the TextView Value
View. setText ("this is a Toast prompt ");
// Set the layout of TextView
View. setLayoutParams (new LayoutParams (LinearLayout. LayoutParams. WRAP_CONTENT, LinearLayout. LayoutParams. WRAP_CONTENT ));
// Toast carries
TextViewtoast. setView (view );
// Display Toast
Toast. show ();

2. The above method is too complex. Generally, it is only applicable to the Toast construction of special interfaces. If you only want to make text prompts, you can use the factory method, it automatically constructs a Toast with borders and text:Copy codeThe Code is as follows: // construct a simple Toast using the factory method, and directly prompt the chain structure
Toast. makeText (this, "this is a Toast prompt", Toast. LENGTH_LONG). show ();

Conclusion: Toast is one of the most common and simple Android controls. Its Auto-closing function greatly simplifies the amount of code and is the best choice for user prompts.

Related Article

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.