Implementation of Android development-toast controls

Source: Internet
Author: User

    • Toast Toast:

Toast content is simple, not too much to introduce, toast support comes with a simple toast, custom toast. Content simple visible code, see API. A toast provides simple feedback about an operation in A small popup. It is fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an e-mail before you send it triggers a "Draft saved" toast-let-you-know that can Continue editing later. Toasts automatically disappear after a timeout.

    • code example

Activity_main.xml

    

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Mainactivity ">Ten  One<Button AAndroid:id= "@+id/button1" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theAndroid:layout_alignparenttop= "true" -Android:layout_centerhorizontal= "true" -android:layout_margintop= "190DP" -android:text= "Toast button"/> +  -<Button +Android:id= "@+id/button2" AAndroid:layout_width= "Wrap_content" atandroid:layout_height= "Wrap_content" -android:layout_alignleft= "@+id/button1" -android:layout_below= "@+id/button1" -android:layout_margintop= "20DP" -android:text= "Custom Toast"/> -  in</RelativeLayout>

Toast_layout.xml

    

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/toast_layout_root"android:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:padding= "8DP"Android:background= "#DAAA" > <imageview android:src= "@drawable/droid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginright= "8DP"/> <textview android:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#FFF"/></linearlayout>

Activity

1  PackageCom.example.android_toast;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.view.Gravity;6 ImportAndroid.view.Menu;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.view.ViewGroup;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.TextView; A ImportAndroid.widget.Toast; - /** -  * @authorXiaowu the * Note:toast toast, support comes with simple toast, custom toast. Content simple visible code, see API -  */ -  Public classMainactivityextendsActivity { -     Privatebutton button; +     PrivateButton button2; - @Override +     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -Button = (Button) This. Findviewbyid (r.id.button1); -Button2 = (Button) This. Findviewbyid (r.id.button2); -          -         //Add a Click Listener event for a button -Button.setonclicklistener (NewView.onclicklistener () { in @Override -              Public voidOnClick (View v) { to                 //TODO auto-generated Method Stub +                 //Toast.maketext (mainactivity.this, "Toast Content", 1). Show ();  -                 //Create a Toast object theToast Toast =toast.maketext (mainactivity. This, "Toast content", 0); *                 //set where Toast appears in the view $Toast.setgravity (gravity.bottom| Gravity.center, 0, 0);Panax Notoginseng toast.show (); -             } the         }); +          A         //Add a Click Listener event for a button theButton2.setonclicklistener (NewOnclicklistener () { + @Override -              Public voidOnClick (View v) { $                 //TODO auto-generated Method Stub $                 //Load a custom toast view from an XML configuration file -View view =getlayoutinflater (). Inflate (R.layout.toast_layout, (ViewGroup) Findviewbyid (r.id.toast_layout_root)); -                 //Use this view to find objects in the view TextView theTextView TextView =(TextView) View.findviewbyid (r.id.text); -Textview.settext ("Toast content");Wuyi //Toast toast = new Toast (mainactivity.this); theToast toast =NewToast (Getapplicationcontext ()); -                 //set where Toast appears in the view WuToast.setgravity (gravity.center_vertical, 0, 0); -                 //setting a time limit for toast About toast.setduration (toast.length_long); $ Toast.setview (view); - toast.show (); -             } -         }); A          +     } the  - @Override $      Public BooleanOncreateoptionsmenu (Menu menu) { the         //inflate the menu; This adds items to the action bar if it is present. the getmenuinflater (). Inflate (R.menu.main, menu); the         return true; the     } -  in}

Implementation of Android development-toast controls

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.