Android Learning (18) use of toast

Source: Internet
Author: User

First, what is toast

1. Toast is a kind of view that provides users with concise and prompt information.

2. The view is presented to the user as floating on the application, and the toast prompt interface does not get the focus and gives the user some hints without affecting the user's use.

3. The toast class provided by Android can create and display toast information.

The following shows the usage of 4 toasts, the normal toast, changing the location of the toast, displaying the toast of the picture, and customizing the toast using 4 buttons respectively.

Main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity ">    <ButtonAndroid:id= "@+id/btnshow"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Toast Display"/>    <ButtonAndroid:id= "@+id/btnshow2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Toast Display location"/>    <ButtonAndroid:id= "@+id/btnshow3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Toast Display Picture"/>    <ButtonAndroid:id= "@+id/btnshow4"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Custom toast display layout file"/></LinearLayout>

Main.java

 PackageCom.example.zhengcheng.myapp;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Button btnshow1;    Button Btnshow2;    Button btnshow3;    Button btnshow4; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Btnshow1=(Button) Findviewbyid (r.id.btnshow); Btnshow1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast= Toast.maketext (mainactivity. This, "Test Toast popup message box", Toast.length_short);            Toast.show ();        }        }); Btnshow2=(Button) Findviewbyid (R.ID.BTNSHOW2); Btnshow2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast= Toast.maketext (mainactivity. This, "Change toast pop-up location", Toast.length_short); //The first parameter sets the relative position of the toast, the second parameter is the X coordinate (positive right, negative left), and the third parameter is the Y coordinate (positive, negative)Toast.setgravity (gravity.center, 0, 250);            Toast.show ();        }        }); BTNSHOW3=(Button) Findviewbyid (R.ID.BTNSHOW3); Btnshow3.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast= Toast.maketext (mainactivity. This, "Toast with pictures Popup dialog box", Toast.length_short); //get how toast is laid outLinearLayout toast_layout =(LinearLayout) Toast.getview (); ImageView IV=NewImageView (mainactivity. This);//Create a picture controlIv.setimageresource (R.mipmap.ic_launcher);//set the display picture//Toast_layout.addview (iv); //Add a picture control to the toast layout, default to the endToast_layout.addview (iv, 0);//Add a picture control to the toast layout, before adding to the textToast.show ();//Show Pictures            }        }); Btnshow4=(Button) Findviewbyid (R.ID.BTNSHOW4); Btnshow4.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//gets the Inflater object that is used to load the viewLayoutinflater inflater = Layoutinflater.from (mainactivity. This); View Toast_view= Inflater.inflate (R.layout.toast_layout,NULL); Toast Toast=NewToast (mainactivity. This);//Create a ToastToast.setview (Toast_view);//set up a toast viewToast.show ();//Show            }        }); }}

customizing Toast Views Toast_layout.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "30DP"android:gravity= "Center"Android:text= "Custom Toast dialog box" />    <ImageViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:src= "@android:d rawable/sym_def_app_icon" />    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "Show toast popup content" /></LinearLayout>

Android Learning (18) use of toast

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.