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
I'm using SDK 2.2.
1. Default display mode
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), or milliseconds such as 2000ms toast Toast=toast.maketext ( Getapplicationcontext (), "Default Toast", Toast.length_short); // Show toast Information Toast.show ();
2. Customizing the display location
Toast Toast=toast.maketext (Getapplicationcontext (), "toast with custom display location" // first parameter: sets the location where the toast appears on the screen. My current settings are centered on the top // // The third argument: same as the second argument. // If you set an offset that exceeds the range of the screen, the toast will display toast.setgravity (gravity.top|) near the outside edge of the screen. Gravity.center, -50, 100); // center screen, x-axis and y-axis offset are 0 // toast.setgravity (gravity.center, 0, 0); Toast.show ();
3. With the picture
Toast Toast=toast.maketext (Getapplicationcontext (), "Show toast with pictures", 3000); Toast.setgravity (Gravity.center,0, 0); //Create a picture View objectImageView imageview=NewImageView (Getapplicationcontext ()); //set up a pictureImageview.setimageresource (R.drawable.ic_launcher); //Get the toast layoutLinearLayout Toastview =(LinearLayout) Toast.getview (); //set this layout to landscapetoastview.setorientation (linearlayout.horizontal); //add ImageView to the first position in this layoutToastview.addview (ImageView, 0); Toast.show ();
4. Fully customizable display mode
//inflater means inflatable .//Layoutinflater This class is used to instantiate the layout of an XML file to its corresponding view objectLayoutinflater Inflater =Getlayoutinflater (); //populate a View object by developing an XML file and layout IDView layout =inflater.inflate (R.layout.custom2, (ViewGroup) Findviewbyid (r.id.lltoast)); ImageView Image=(ImageView) Layout.findviewbyid (r.id.tvimagetoast); //To set a picture in a picture view in a layoutImage.setimageresource (R.drawable.ic_launcher); TextView title=(TextView) Layout.findviewbyid (r.id.tvtitletoast); //Set TitleTitle.settext ("title bar"); TextView text=(TextView) Layout.findviewbyid (r.id.tvtexttoast); //Set ContentText.settext ("Fully customizable toast"); Toast Toast=NewToast (Getapplicationcontext ()); Toast.setgravity (Gravity.center,0, 0); Toast.setduration (Toast.length_long); Toast.setview (layout); Toast.show ();
5. Calls from other threads through Handler
// call Method 1 // thread th=new Thread (this); // th.start (); // call Method 2 handler.post (new Runnable () {@Override public void run () {showtoast () ; } });
Public void Showtoast () { toast toast=toast.maketext (Getapplicationcontext (), "Toast calls display in other threads", Toast.length _short); Toast.show (); }
Handler handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {intwhat=Msg.what; Switch(what) { Case1: Showtoast (); Break; default: Break; } Super. Handlemessage (msg); } };
@Override Public void run () { handler.sendemptymessage (1); }
Transferred from: http://daikainan.iteye.com/blog/1405575
Android Application Development Learning-toast use method Daquan