Android uses toast to display a message prompt box

In the previous example, the toast class has been applied to display a simple prompt box. Toast will be introduced in detail this time. Toast class is used to display a message prompt box in the screen. The message prompt box does not have any control buttons and will not get focus. It will disappear automatically after a period of time. It is usually used to display some quick prompt information, and has a wide range of applications.

Using toast to display the message prompt box is very simple. You only need the following three steps:

(1) . create a toast object. There are usually two methods: one is to create using the construction method; Toast toast=new Toast(this); The other is to call the maketext () method of toast class to create. Toast toast = toast.maketext (this, "content to be displayed", toast. Length_short);

(2) . call the methods provided by the toast class to set the alignment, margin, displayed content, etc. of the message prompt box. The common methods are as follows: setduration (int duration) is used to set the duration of the message prompt box. The parameter usually uses toast.length_ Long or toast.length_ SHORT

Setgravity (int gravity, int xoffset, int yoffset) is used to set the position of the message prompt box, and the parameter grivaty is used to specify the alignment: xoffset and yoffset are used to specify the specific offset value

Setmargin (float horizontal margin, float vertical margin) is used to set the margin of the message prompt

Settext (charsequences) is used to set the text content to be displayed

Setview (view) is used to set the view to be displayed in the prompt box

(3) . call the show () method of toast class to display a message prompt box. It should be noted that this method must be called, otherwise the set message prompt box will not be displayed.

The following is a specific example to illustrate how to use toast class to display message prompt box.

res/layout/main.xml:

MainActivity:

The effect is shown in the figure:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>