In the event, the Findviewbyid () method can be used to get to the element defined in the layout file, where we pass in r.id.button_1 to get an instance of the button, which was specified in the First_layout.xml by the Android:id attribute just now. The Findviewbyid () method returns a View object, which we need to transform downward to turn it into a button object. After getting an instance of the button, we register a listener for the button by calling the Setonclicklistener () method, and the OnClick () method in the listener is executed when the button is clicked. Therefore, the ability to eject a toast is, of course, written in the onclick () method.
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 a request for a toast, because the activity itself is a contextual object, so it can be passed directly to Firstactivity.this. The second parameter is the text content of the toast display, the third parameter is the length of the toast display, and there are two built-in constants to choose Toast.length_short and Toast.length_long. Example
Button button1 = (button) Findviewbyid (r.id.button_1);
Button1.setonclicklistener (New View.onclicklistener ()
{
public void OnClick (View v) {
Toast.maketext (Firstactivity.this, "You clicked Button 1", Toast.length_short). Show ();
}
}
);
Usage of toast in Android