Today I learned a few ways to share the usage of Toast .
Toast is very simple to use, first of all to introduce a toast. Toast is something I met when I was writing myclock, and I found out that it was an excellent reminder for Android, and it can be used to inform users of short messages in a program.
These messages automatically disappear after the click, and do not occupy the screen space. The following code:
First: Define a toast's touch point, and the following implements a button to make a toast touch point.
Activity_main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.example.jwg.firstapp.MainActivity"> One <Button A Android:id= "@+id/button1" - Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" /> the </Relativelayout>
Second: Mainactivity.java
1 protected voidonCreate (Bundle savedinstancestate) {2 3 Super. OnCreate (savedinstancestate);4 5 requestwindowfeature (window.feature_no_title);6 7 Setcontentview (r.layout.first_layout);8 9Button button1 =(Button) Findviewbyid (r.id.button_1);Ten OneButton1.setonclicklistener (NewOnclicklistener () { A - @Override - the Public voidOnClick (View v) { - -Toast.maketext (firstactivity. This, "You clicked Button 1", - + toast.length_short). Show (); - + } A at }); - -}
It's so simple!
Summarize:
A toast is very simple to use, creating a Toast object with a static method Maketext () and then calling Show () to display the toast.
It is important to note that the Maketext () method needs to pass in three parameters. The first parameter is the context, which is what the toast requires, because
The activity itself is a context object, so it can be passed directly to Firstactivity.this. The second parameter is the text content of the toast display, and the first
Three parameters are the duration of the Toast display, with two built-in constants to choose from Toast.length_short and Toast.length_long
The Toast of Android development