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.