Android Study Notes (30th): pop-up information-Toast and alarm

Source: Internet
Author: User

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

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.