Use of toast (toast) in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.