Android instances in simple output series-use plain text for pop-up message Toast objects (1)

Source: Internet
Author: User

  Reading directory

I. Toast

  II. Implementation steps

I. Toast

Toast is the object of the Android-specific prompt information. It is very simple to use, but has a wide range of uses. Toast is a short message, the information to be told is displayed as a View floating in the top layer. After the View is displayed, it will automatically disappear after several seconds. With the Toast feature, it can display User information without affecting user calls or listening to music. For our developers, it is also a very easy-to-use Debug method. You can use Toast when running the program, display running variables and other information

You can enter the text in the EditText control and click the button to send the Toast message.

  II. Implementation steps

1. layout file writing

1.1: Layout file main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: singleLine = "true"
Android: id = "@ + id/et"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "get information"
Android: id = "@ + id/btn1"
/>

</LinearLayout>

2: code file writing

2. 1: MainActivity. java

Package com. menglin. toast;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;

Public class MainActivity extends Activity
{
// Declare a Button Object
Private Button mybtn = null;
// Declare an EditText object
Private EditText myedittext = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );

// Load the main. xml layout File
SetContentView (R. layout. main );
// Obtain the Button object using the findViewById () method
Mybtn = (Button) findViewById (R. id. btn1 );
// Obtain the EditText object using the findViewById () method
Myedittext = (EditText) findViewById (R. id. et );
// Bind the Button object to a click listener event
Mybtn. setOnClickListener (listener );
}
 
// Listen for events
Private OnClickListener listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
Editable str; // declare string variables
// Obtain the text content of EditText entered by the user
Str = myedittext. getText ();
// Create a Toast object using the static method makeText () of Toast. The parameters of this method are the context, displayed text, and displayed length of time, the display time can also be set to Toast. LENGTH_SHORT, the display time is relatively short, and then the Toast is displayed by calling the show () method. makeText (MainActivity. this, str. toString (), Toast. LENGTH_LONG ). show ();
// Clear EditText
Myedittext. setText ("");
}
};
}

  Iii. Running Effect

When we enter text in EditText and click the button, a prompt is displayed.

  

  

 

 

Related Article

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.