The Toast of Android learning notes

Source: Internet
Author: User

1. Post an android Api-toast

Toast

public class Toast
Extends Object

Java.lang.Object

?

Android.widget.Toast

A toast is a view containing a quick little messagefor the user. The Toast class helps you create and show those.

When the view was shown to the user, appears as Afloating view over the application. It'll never receive focus. The user willprobably is in the middle of typing something else. The idea was to being asunobtrusive as possible, while still showing the user the information you wantthem to see. The examples is the volume control, and the brief message sayingthat your settings has been saved.

The easiest-on-the-use-this-class is-to-call one ofthe static methods, constructs everything you need and returns a NE W Toastobject.

Developer Guides

For information about creating toasts Notifications,read the Toast Notifications Developer Guide.

Summary

Constants

Int

Length_long

Show the view or text notification for a long period of time.

Int

Length_short

Show the view or text notification for a short period of time.

Public constructors

Toast (Context context)

Construct an empty Toast object.

Public methods

void

Cancel ()

Close The view if it's showing, or don ' t show it if it's isn ' t showing yet.

Int

Getduration ()

Return the duration.

Int

Getgravity ()

Get the notification should appear on the which.

Float

Gethorizontalmargin ()

Return the horizontal margin.

Float

Getverticalmargin ()

Return the vertical margin.

View

GetView ()

Return to the view.

Int

Getxoffset ()

Return the X offset in pixels to apply to the gravity's location.

Int

Getyoffset ()

Return the Y offset in pixels to apply to the gravity's location.

Statictoast

Maketext (context context, int resId, int duration)

Make a standard toast, the just contains a text view with the text from a resource.

Statictoast

Maketext (Context context, charsequence text, int duration)

Make a standard toast, that just contains a text view.

void

Setduration (int duration)

Set how long to show the view for.

void

setgravity (int gravity, int xoffset, int yoffset)

Set the location at which the notification should appear on the screen.

void

SetMargin (float horizontalmargin, float verticalmargin)

Set the margins of the view.

void

SetText (int resId)

Update the text in a toasts that is previously created using one of the Maketext () methods.

void

SetText (charsequence s)

Update the text in a toasts that is previously created using one of the Maketext () methods.

void

Setview (View view)

Set the view to show.

void

Show ()

Show The View for the specified duration.

Inherited methods

From class Java.lang.Object

2. Chinese Introduction and Usage example

Toast is a mechanism used to display information in Android, unlike dialog, where toast is not focused, and toast is displayed with limited time, and will automatically disappear after a certain amount of time. Here's an example to see how to use toast.

2.1 Default Effect

Code

Toast.maketext (Getapplicationcontext (), default Toast style,   toast.length_short). Show ();
2.2Customizing display position effects

Code

Toast = Toast.maketext (Getapplicationcontext (),     "Custom location Toast", Toast.length_long);   Toast.setgravity (gravity.center, 0, 0);   Toast.show ();

2.3 With picture effect

Code

Toast = Toast.maketext (Getapplicationcontext (),     "toast with pictures", toast.length_long);   Toast.setgravity (gravity.center, 0, 0);   LinearLayout Toastview = (linearlayout) toast.getview ();   ImageView imagecodeproject = new ImageView (Getapplicationcontext ());   Imagecodeproject.setimageresource (R.drawable.icon);   Toastview.addview (imagecodeproject, 0);   Toast.show ();

2.4 Fully customizable effects

Code

Layoutinflater inflater = Getlayoutinflater ();   View layout = Inflater.inflate (R.layout.custom,     (viewgroup) Findviewbyid (r.id.lltoast));   ImageView image = (ImageView) layout     . Findviewbyid (r.id.tvimagetoast);   Image.setimageresource (R.drawable.icon);   TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);   Title.settext ("Attention");   TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);   Text.settext ("Fully customizable Toast");   Toast = new Toast (Getapplicationcontext ());   Toast.setgravity (Gravity.right | Gravity.top, (a);   Toast.setduration (Toast.length_long);   Toast.setview (layout);   Toast.show ();

2.5 Other Threads

Code

New Thread (New Runnable () {public    void run () {     showtoast ();    }   }). Start ();

The Toast of Android learning notes

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.