In some cases, a prompt message, such as an error message or a short message, needs to be displayed. Android provides two pop-up methods: toasts and alerts.
Toast is a short message prompt. It automatically disappears after a period of time without requiring user interaction. Therefore, it is used to display less important messages created by the token, if you are prompted that a background task is completed.
It is also very easy to use toast to pop up the prompt message. Call the static method maketext () of toast class ():
Public static toast maketext (context, charsequence text, int duration)
Context: The call context, which is usually an application or activity object.
Text: displayed message
Duration: the length of time displayed, Which is toast. length_long or toast. length_short.
Call toast. maketext (this, "deleted successfully! ", Toast. length_long). Show (); the effect is as follows:
Alertdialog is similar to the traditional mode dialog box. It is closed only after it interacts with the user.
The simplest way to create the alertdialog dialog box is to use the nested class builder of alertdialog. It has the following main methods:
Setmessage (): Set the displayed message content
Settitle () and seticon (): Set the text and icon in the title bar of the dialog box.
Setpositivebutton (), setneutralbutton (), and setnegativebutton (): Set the buttons in the dialog box, including the text displayed by the button and the events clicked by the button.
Setview (): The Settings dialog box displays a custom view.
Custom view addemployee. xmlCodeNote that the name of the layout file can only contain "a-z0-9 _.", otherwise the following error will be reported: "invalid file name: must contain only [a-z0-9 _.]"
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "name:"/> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: id = "@ + ID/editname"> </edittext> </linearlayout> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Age:" ></textview> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/editage" Android: inputtype = "Number"> </edittext> </linearlayout>
The code for generating the dialog box is as follows:
Layoutinflater = layoutinflater. from (this); viewaddemployee = layoutinflater. inflate (R. layout. addemployee, null); New alertdialog. builder (this ). settitle ("add employee "). setview (viewaddemployee ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {insertemployee ();}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which ){}}). show ();
A custom view is loaded here, and the custom view is displayed in the setview () Setting Dialog Box. Two buttons and corresponding click events are added. The running effect is as follows:
I hope this article will help you.
Reference books: Beginning Android 2 and Android official documents