One: Look at the program
Second: Change the layout:
1: Under res resources, find layout, open Activity_main.xml
Below the graphical layout view, you can customize the drag interface elements to adjust the layout. For example, add a button
2: Open activity_main.xml View, edit source code, add Trigger function for button
3: Adding functions to activity
public void Test (view view) {System.out.println ("------test----"); Toast toast = Toast.maketext (Mainactivity.this, "clicked Button", Toast.length_short); The screen is centered, and the x-axis and y-axis offsets are 0 toast.setgravity (gravity.center, 0, 0); Toast.show ();}
Three: Start the simulator
When you click the button, an informational prompt appears.
Four: Usage of toast
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
Java code
- //First parameter: the current context. Available Getapplicationcontext () or this
- ///second parameter: the string to display. It's also a string ID in r.string.
- ///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
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 to left offset
- ///third parameter: Same as the second argument
- //If you set an offset that exceeds the range of the screen, the toast will appear in the screen near the outside border
- Toast.setgravity (gravity.top| Gravity.center,--- -);
- //center display, x-axis and y-axis offset are 0
- //toast.setgravity (gravity.center, 0, 0);
- Toast.show ();
3. With the picture
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 the toast layout
- LinearLayout Toastview = (linearlayout) Toast.getview ();
- //Set this layout to landscape
- Toastview.setorientation (linearlayout.horizontal);
- //ImageView is added to the first position in this layout
- Toastview.addview (ImageView, 0);
- Toast.show ();
4. Fully customizable display mode
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 picture in Picture view in Layout
- Image.setimageresource (R.drawable.ic_launcher);
- TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
- //Set title
- 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
Java code
- //Call Method 1
- //thread th=new Thread (this);
- //th.start ();
- //Call Method 2
- Handler.post (new Runnable () {
- @Override
- Public void Run () {
- Showtoast ();
- }
- });
Java code
- Public void Showtoast () {
- Toast Toast=toast.maketext (Getapplicationcontext (), "toast calls in other threads to display", toast.length_short);
- Toast.show ();
- }
Java code
- 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);
- }
- };
Java code
- @Override
- Public void Run () {
- Handler.sendemptymessage (1);
- }
Android Basics: HelloWorld Toast Usage