A Toast is a view that quickly displays a small amount of information to the user. A Toast floats on the application to display information to the user, it never gets the focus, does not affect the user's input, and so on, mainly for some help/hints.
The most common way to create a Toast is to use a static method Toast.maketext
1. Default display mode
Background Java code:
First parameter: The current context environment. Available Getapplicationcontext () or this //second parameter: the string to display. It's also a r.string. String ID //third parameter: The length of time displayed. Toast defaults to two length_long (long) and Length_short (short), and can also be used in milliseconds such as 2000ms toast Toast=toast.maketext (Getapplicationcontext () , "Default Toast", Toast.length_short); Show Toast Information toast.show ();
2. Customizing the display location
Background Java code:
Toast Toast=toast.maketext (Getapplicationcontext (), "toast with custom display location", toast.length_short);//First parameter: Sets the location where the toast appears on the screen. My current settings are centered on the top//second parameter: The offset of the horizontal x-axis of the toast position relative to the first parameter, positive offset to the right, negative offset to the left//third parameter: offset from the vertical y-axis of the toast position set relative to the first parameter, positive offset upward, negative number downward offset
If you set an offset that exceeds the range of the screen, the toast will display toast.setgravity near the outside edge of the screen (gravity.top| Gravity.center,-50, 100); The screen is centered and the x-axis and y-axis offsets are 0//toast.setgravity (gravity.center, 0, 0); Toast.show ();
3. With the picture
Background Java code:
Toast Toast=toast.maketext (Getapplicationcontext (), "Show toast with pictures", "toast.setgravity" (gravity.center, 0, 0); Create picture View object ImageView imageview= new ImageView (Getapplicationcontext ());//Set Picture Imageview.setimageresource ( R.drawable.ic_launcher);//Get toast layout linearlayout Toastview = (linearlayout) toast.getview ();// Set this layout to Landscape toastview.setorientation (linearlayout.horizontal);//Will imageview the first position added to this layout Toastview.addview ( ImageView, 0); Toast.show ();
4. Fully customizable display mode
Background Java code:
Inflater means inflatable//layoutinflater This class is used to instantiate the layout of an XML file to its corresponding View object Layoutinflater Inflater = Getlayoutinflater ();// Populate a View object by developing an XML file and layout id View layout = inflater.inflate (R.layout.custom2, (ViewGroup) Findviewbyid (r.id.lltoast)); ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast);//Set layout in Picture view in Picture Image.setimageresource ( R.drawable.ic_launcher); TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);//Set Caption Title.settext ("title bar"); TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);//set Content Text.settext ("fully customizable Toast"); Toast Toast= New Toast (Getapplicationcontext ()); Toast.setgravity (gravity.center, 0, 0); Toast.setduration ( Toast.length_long); Toast.setview (layout); Toast.show ();
5. Calls from other threads through Handler
Background Java code:
[1] Instantiation of Handler
Handler handler=new Handler () {
@Override
public void Handlemessage (Message msg) {
int what=msg.what;
Switch (what) {
Case 1:
Showtoast ();
Break
Default
Break
}
Super.handlemessage (msg);
}
};
[2] Toast Display
public void Showtoast () { toast Toast=toast.maketext (Getapplicationcontext (), "Toast calls display in other threads", toast.length_ short); Toast.show ();}
[3] Calling method
Calling Method 1
Thread Th=new thread (this);
Th.start ();
Calling Method 2
Handler.post (New Runnable () {
@Override
public void Run () {
Showtoast ();
}
});
[4] Putting data
@Overridepublic void Run () { handler.sendemptymessage (1);//Data only, SendMessage () allows you to process a Message object (the message can contain data). }
The Toast of Android