Android provides two pop-up boxes for commonly used messages: Toast and Alert.
Toast
Toast is a short prompt box that does not require user interaction or focus shifting. Therefore, it is suitable for most scenarios and provides information to users. Toast has been used for many times in previous studies.
It is very easy to create a Toast. Use the static method makeText (Context context, CharSequence text | int resId, int duration) to convert String (or String ID ), and the length of time (LENGTH_SHORT or LENGTH_LONG) to get a Toast object.
The above is the most common method, but if you want to display it as another view, not just text, you can use Toast constructor Toast (Context context), and then use setView () set the displayed view and set the display time through setDuration.
To display Toast, call the show () method. For example:
Toast.MakeText(This ,"<Clink, clink>", Toast. LENGTH_SHORT ).Show();
Alert
The traditional method is in the form of dialog box, which requires AlertDialog. When a warning box pops up, the focus is obtained and must be disabled by the user to display important errors or events, make sure that the user knows the scenario or some verification information.
The easiest warning box is passed through AlertDialog. you can call setMessage () to set the display text content, setTitle (), setIcon (), and setPositiveButton (),
SetNeutralButton (),
SetNegativeButton (). These buttons are not associated with their names. They only indicate that their positions are in sequence with the left, right, and a maximum of three buttons are displayed, we need to set the display content and click trigger processing of these buttons. Finally, call the show () method to display it.
If you need to create a builder object first, configure it, and display it again, you can first use create (), then set the above content, and finally call show (). Once show () is called, it will always wait for the user to process it.
The following is an example of a warning box. We set the content of the alarm box in sequence:
NewAlertdialog. Builder(This)
.Settitle("Alerting Message ")
.Setmessage("Eek! ")
.Setnegativebutton("Close", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Do nothing-it will close on its own
}
})
.Show();
When viewing the reference, you can set setItems (), setMultiChoiceItems (), and setSingleChoiceItems () for AlertDialog. Builder. The following is an example:
New AlertDialog. Builder (this)
. SetTitle ("Select one :")
.Setsinglechoiceitems(R. array. colors, 1, new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Do nothing-it will close on its own
}
})
. Show ();
Related Links: My Andriod development articles