Learn android from scratch (Toast prompt component. 20 .)
In the system, some user operations can be prompted through the dialog box, but the Android platform also provides a more friendly display interface effect, in addition, this interface does not interrupt normal operations when prompted to the user. This dialog box can be implemented through the Toast component. Toast is a component that displays operations with simple prompts. In Android, the inheritance structure of android. widget. Toast is as follows: java. lang. Object? Common android. widget. Toast methods:
No. |
Methods and constants |
Type |
Description |
1 |
Public static final int LENGTH_LONG |
Constant |
Long display time |
2 |
Public static final int LENGTH_SHORT |
Constant |
Short display time |
3 |
Public Toast (Context context) |
Normal |
Create a Toast object |
4 |
Public static Toast makeText (Context context, int resId, int duration) |
Normal |
Create a Toast object and specify the ID and display time of the text resource. |
5 |
Public static Toast makeText (Context context, CharSequence text, int duration) |
Normal |
Create a Toast object and specify the display time of text resources and information. |
6 |
Public void show () |
Normal |
Display Information |
7 |
Public void setDuration (int duration) |
Normal |
Set the display time |
8 |
Public void setView (View view) |
Normal |
Set the displayed View component |
9 |
Public void setText (int resId) |
Normal |
Set the displayed text resource ID |
10 |
Public void setText (CharSequence s) |
Normal |
Directly set the text to be displayed |
11 |
Public void setGravity (int gravity, int xOffset, int yOffset) |
Normal |
Set the alignment of components |
12 |
Public View getView () |
Normal |
Obtain the internal View component |
13 |
Public int getXOffset () |
Normal |
Returns the X coordinate position of the component. |
14 |
Public int getYOffset () |
Normal |
Returns the Y coordinate position of the component. |
15 |
Public void cancel () |
Normal |
Cancel display |
XMl file configuration
JAVA file configuration
Package com. example. toast; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button button1, button2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2); button1.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // The original prompt Toast. makeText (MainActivity. this, "Custom prompt box", Toast. LENGTH_SHORT ). show () ;}}); // click the event button2.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubView view = LayoutInflater. from (MainActivity. this ). inflate (R. layout. linearlayout, null); // converts Layout to View object Toast toast = new Toast (MainActivity. this); // The Toast prompt is displayed when toast is created. setGravity (Gravity. CENTER, 0, 0); // set Toast. setView (view); // Add the layout to the Toast component. show (); // display prompt }});}}
Original prompt effect:
Custom toast display
The Toast component is relatively simple. It is mainly used to prompt user-friendly operations and is often used in development. I hope you can master it.
Next prediction: ListView data list display component