Use of toast in Android
What is toast (toast)?
Toast is a great way for Android to prompt you to use it to notify users of short messages that will automatically disappear over time and will not occupy any screen space.
Let's use the code to experience the usage of toast in Android.
1 Importandroid.app.Activity;2 Importandroid.content.Intent;3 ImportAndroid.os.Bundle;4 ImportAndroid.view.Menu;5 ImportAndroid.view.MenuItem;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.view.Window;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.Toast; One A - Public classMainactivityextendsActivity { - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - + //does not display the title bar in the active window, this line of code must be placed before Setcontentview (R.layout.activity_main), or an error will be -requestwindowfeature (window.feature_no_title); A at //load a layout resource - Setcontentview (r.layout.activity_main); - - //get a control---> button -Button btn = (button) This. Findviewbyid (r.id.button1); - in //set the contents of a button -Btn.settext ("Click Me"); to + //Add a Click event to a button
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 function of the popup Toast is of course to be written in the OnClick () method -Btn.setonclicklistener (NewMyonclicklistener ()); the
- }Wuyi the /** - * Use of toast in Android */ $ Private classMyonclicklistenerImplementsonclicklistener{
/**
* Context Contextual Object
* If in Toast.maketext (this, "", ""), here the this represents the Myonclicklistener Object
* We want to get to the context, where mainactivity inherits the activity, and it inherits the context from the bottom.
* so we can get through the class name. this
* Text to display
* Duration Display Duration
Note: You also need to call the method in the Toast Show () method to display the prompt message
*/
A @Override Public void OnClick (View v) { Toast.maketext (mainactivity. this, "Hello!" , Toast.length_long). Show (); - }
*
Use of toast (toast) in Android