Toast Message Prompt

Source: Internet
Author: User

Four types, normal, change position, with diagram, custom

First, the layout is built with four buttons corresponding to each other.

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"android:orientation= "vertical"        >        <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/toast_btn1"Android:text= "Show Toast"            />                <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/toast_btn2"Android:text= "Change position toast"            />                <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/toast_btn3"Android:text= "With graph toast"            />                <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/toast_btn4"Android:text= "Full Custom toast"            />    </LinearLayout></Relativelayout>

Then create a new layout to put the custom

<?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= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "Static Flow"        />    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/four"        />    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "self-defined static flow" is mine.        /></LinearLayout>

Then write the master file

 PackageCom.example.deemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateView toast_layout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main);    Initevent (); }        Private voidInitevent () {//Initialize a click event (response event)Findviewbyid (R.ID.TOAST_BTN1). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {showToast1 ();        }        }); Findviewbyid (R.ID.TOAST_BTN2). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {showToast2 ();        }        }); Findviewbyid (R.ID.TOAST_BTN3). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {showToast3 (); }}); Findviewbyid (R.ID.TOAST_BTN4). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {showToast4 ();    }        }); }    Private voidShowToast1 () {//Show Default ToastToast toast = Toast.maketext ( This, "Static flow is my wife", Toast.length_long);//the last one is time .toast.show (); //Toast.maketext (This, "default", Toast.length_long). Show ();//write a different way to play ...     }    Private voidShowToast2 () {//Show custom Locations ToastToast toast = Toast.maketext ( This, "Change the position, or my wife.", Toast.length_long); Toast.setgravity (Gravity.top,0, 100);    Toast.show (); }    Private voidShowToast3 () {//with a pictureToast toast = Toast.maketext ( This, "Look at my wife!", Toast.length_long); LinearLayout Toast_layut=(LinearLayout) Toast.getview (); ImageView IV=NewImageView ( This);        Iv.setimageresource (R.drawable.four); Toast_layut.addview (iv,1);//The second parameter is the location where you can change thetoast.show (); }    Private voidShowToast4 () {//fully customizableLayoutinflater Inflater = Layoutinflater.from ( This); View Toast_view= Inflater.inflate (R.layout.toast_layout,NULL); Toast Toast=NewToast ( This);        Toast.setview (Toast_view);    Toast.show (); }}

Here the button with the response to the event, actually carry out the initialization and then use the internal class also can it.

Toast Message Prompt

Related Article

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.