ANDROID_ (message tip) multiple message hints for using toast

Source: Internet
Author: User

A toast is a view that provides information about a user and can create and display a message that is presented to the user as it floats above the application. because it does not get the focus, even if the user is entering nothing will be affected .

The toast goal is to make it as unobtrusive as possible so that the user sees the information that is prompted.

Run:

Program Structure

 Packagecom.example.asus.gary_74;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateButton Bt1,bt2,bt3,bt4; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BT1=(Button) Findviewbyid (R.id.button); Bt1.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//toast.length_short approx. 2 sec.//Toast.length_long approx. 3.5 sec.Toast toast = Toast.maketext (mainactivity. This, "Default Toast", Toast.length_long);            Toast.show ();        }        }); BT2=(Button) Findviewbyid (R.id.button2); Bt2.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast=toast.maketext (Getapplicationcontext (), "Customize Toast for 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: offset from 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 borderToast.setgravity (gravity.top| Gravity.center,-50, 100); //the screen is centered and the x-axis and y-axis offsets are 0//toast.setgravity (gravity.center, 0, 0);toast.show ();        }        }); BT3=(Button) Findviewbyid (R.id.button3); Bt3.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast=toast.maketext (Getapplicationcontext (), "show Toast with pictures", Toast.length_long); Toast.setgravity (Gravity.center,0, 0); //Create a picture View objectImageView imageview=NewImageView (Getapplicationcontext ()); //set up a pictureImageview.setimageresource (Android.                 R.drawable.ic_input_add); //Get the toast layoutLinearLayout Toastview =(LinearLayout) Toast.getview (); //set this layout to landscapetoastview.setorientation (linearlayout.horizontal); //add ImageView to the first position in this layoutToastview.addview (ImageView, 0);            Toast.show ();    }        }); }}
mainactivity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context= "Com.example.asus.gary_74.MainActivity" > <TextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "gary_toast!"android:textsize= "40px"/> <Button Android:id= "@+id/button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Default display"/> <Button Android:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Custom location display"/> <Button Android:id= "@+id/button3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "with picture display"/></linearlayout>
Activity_main.xml

First, the interface layout

Three buttons to display different toast cue effects, respectively

II. Realization of program function

1. Display the default Toast message

 toast maketext (Context context,int resid,int duration)  

context: used contexts, typically activity objects

resId: the string resource ID to use

duration: The time that the message stays on the screen. The value is Length_short or Length_long
  bt1=(Button) Findviewbyid (R.id.button);        Bt1.setonclicklistener (new  Button.onclicklistener ()        {            @Override            public  void  OnClick (View v) {                //toast.length_short approx. 2                sec. //  Toast.length_long  about 3.5 seconds                Toast toast = Toast.maketext (mainactivity.  this, "Default Toast", Toast.length_long);                Toast.show ();            }        });

2. Custom Location Display Toast message

void setduration (int gravity,int xoffset,int yofset)
Set the location where the prompt message appears on the screen
Bt2=(Button) Findviewbyid (R.id.button2); Bt2.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast=toast.maketext (Getapplicationcontext (), "Customize Toast for 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: offset from 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 borderToast.setgravity (gravity.top| Gravity.center,-50, 100); //the screen is centered and the x-axis and y-axis offsets are 0//toast.setgravity (gravity.center, 0, 0);toast.show (); }        });

3. Displaying toast messages with pictures

bt3=(Button) Findviewbyid (R.id.button3); Bt3.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Toast Toast=toast.maketext (Getapplicationcontext (), "show Toast with pictures", Toast.length_long); Toast.setgravity (Gravity.center,0, 0); //Create a picture View objectImageView imageview=NewImageView (Getapplicationcontext ()); //set up a pictureImageview.setimageresource (Android.                 R.drawable.ic_input_add); //Get the toast layoutLinearLayout Toastview =(LinearLayout) Toast.getview (); //set this layout to landscapetoastview.setorientation (linearlayout.horizontal); //add ImageView to the first position in this layoutToastview.addview (ImageView, 0);            Toast.show (); }        });

Portal: Message mode Toast.make Several common uses of text

ANDROID_ (message tip) multiple message hints for using toast

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.