Toast: A great way to remind your Android system that it can be used to notify users of short messages that will automatically disappear after a certain period of time and will not occupy any screen space.
First, you need to define a popup toast trigger point, and then note a program, just above a button, we click on this button when a toast pops up, in the OnCreate () method, add the following code:
protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (r.layout.firstlayout); = (Button) Findviewbyid (r.id.button1); Button.setonclicklistener (new View.onclicklistener () { public void OnClick (view view) { Toast.maketext (firstactivity. This, "You Clicked Button1", Toast.length_short). Show ();}} ); }}
In the event, you can get the elements of the layout file definition through the Findviewbyid () method, where we pass in R.id.button1 and get an instance of the button, which we specified in Firstlayout by the Android:id attribute. After we get an instance of the button, we register a listener for the button by calling the Setclicklistener () method, and the OnClick () method in the listener executes when the button is clicked. Therefore, the ability to eject a toast is, of course, written in the Onclik () method.
The usage of toast is very simple, create a Toast object by static method Maketext (), and then call Show () to display the toast (), here Maketext () passed three parameters, the first parameter is the context, That is, the context of the toast request, because the activity itself is a context object, so it is directly passed into the firstactivity.this. The second parameter is the text content of the toast display, and the third parameter is the length of the toast display, with two built-in constants Toast.length_short and Toast.length_long.
:
Android first line of code learning Note two---using toast in an activity