Android dialog box

Source: Internet
Author: User

Android reminder
There are three main methods for android Notification: Toast Notification, Status Bar Notification, and Dialog notification. Many Dialog notifications are used in Standup Timer. Especially when deleting an item, a confirmation dialog box is displayed. There are four types of Dialog Notification: Alert Dialog, ProgressDialog, DatePickerDialog, and TimePickerDialog. This article focuses on Alert Dialog.

Alert Dialog
Alert Dialog is easy to create. We will create a warning Dialog box with EditText view by following the steps, and create a team that participates in the meeting in Standup timer through this Dialog box.
Step 1: override the onCreateDialog method and create a dialog box with different IDs.
[Java]
@ Override
Protected Dialog onCreateDialog (int id)
{
Switch (id)
{
Case CREATE_TEAM_DIALOG:
If (createTeamDialog = null)
{
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Builder. setTitle (R. string. add_team );
Builder. setView (getTextEntryView ());
Builder. setCancelable (true );
// Set the OK button and listen for its events
Builder. setPositiveButton (R. string. OK, addTeamButtonListener ());
// Set the negative button and listen for its events
Builder. setNegativeButton (R. string. revert, cancelListener ());
CreateTeamDialog = builder. create ();

}
Return createTeamDialog;
Default:
Logger. e ("Attempting to create an unkonwn dialog with an id of" + id );
Returnnull;
}
}

@ Override
Protected Dialog onCreateDialog (int id)
{
Switch (id)
{
Case CREATE_TEAM_DIALOG:
If (createTeamDialog = null)
{
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Builder. setTitle (R. string. add_team );
Builder. setView (getTextEntryView ());
Builder. setCancelable (true );
// Set the OK button and listen for its events
Builder. setPositiveButton (R. string. OK, addTeamButtonListener ());
// Set the negative button and listen for its events
Builder. setNegativeButton (R. string. revert, cancelListener ());
CreateTeamDialog = builder. create ();

}
Return createTeamDialog;
Default:
Logger. e ("Attempting to create an unkonwn dialog with an id of" + id );
Returnnull;
}
} AlertDialog. Builder is used to create a specific warning dialog box.
SetTitle () is the title of the Set dialog box;
SetView (View) is used to load the View for the dialog box. If necessary, an EditText is loaded here.
SetCancelable (boolean) sets whether the return key can be undone dialog box, usually true
SetPositiveButton () is used to set the text of the button and the event listener of the button when the button is pressed.
SetNegativeButton () sets the text and listener of the cancel button
In addition, if the dialog box does not require a special view control, you do not need to use setView (). You can use setMessage (Msg) to display the expected warning information.
After setting Builder attributes, you can create the AlertDialog dialog box through builder. create.
 
Step 2: display the dialog box
The display dialog box is very simple. Add showDialog (id) in the action you need to bring up the reminder. The dialog box is encapsulated by id.
[Java]
Privatevoid displayAddTeamDialog (){
ShowDialog (CREATE_TEAM_DIALOG );
 
}

Privatevoid displayAddTeamDialog (){
ShowDialog (CREATE_TEAM_DIALOG );

} Step 3: Compile button events in the dialog box
This is similar to the event writing of common buttons. Here, the button event is handled by adding a team operation.

[Java]
Private OnClickListener addTeamButtonListener (){

Returnnew DialogInterface. OnClickListener (){
Publicvoid onClick (DialogInterface dialog, int id ){
EditText collectedTextView = (EditText) getTextEntryView (). findViewById (R. id. collected_text );
String name = collectedTextView. getText (). toString ();
Team. create (name, TeamListActivity. this );
TeamListAdapter. add (name );
}
};
}

Private OnClickListener addTeamButtonListener (){

Returnnew DialogInterface. OnClickListener (){
Publicvoid onClick (DialogInterface dialog, int id ){
EditText collectedTextView = (EditText) getTextEntryView (). findViewById (R. id. collected_text );
String name = collectedTextView. getText (). toString ();
Team. create (name, TeamListActivity. this );
TeamListAdapter. add (name );
}
};
} The OnClickListener under DialogInterface is returned.
[Java]
Private OnClickListener cancelListener (){

Returnnew DialogInterface. OnClickListener (){

@ Override
Publicvoid onClick (DialogInterface dialog, int which ){
Dialog. cancel ();

}
};
}

Private OnClickListener cancelListener (){

Returnnew DialogInterface. OnClickListener (){

@ Override
Publicvoid onClick (DialogInterface dialog, int which ){
Dialog. cancel ();

}
};
} In the Undo dialog box, exit the warning.
 
About setView
The getTextEntryView method is called in several parts of the Code. This is a custom method that returns the View. SetView allows you to display the View you want and receive user input information.
[Java]
Synchronizedprotected View getTextEntryView (){
If (txtEntryView = null ){
LayoutInflater factory = LayoutInflater. from (this );
TxtEntryView = factory. inflate (R. layout. collect_text, null );
}
Return txtEntryView;

}

Synchronizedprotected View getTextEntryView (){
If (txtEntryView = null ){
LayoutInflater factory = LayoutInflater. from (this );
TxtEntryView = factory. inflate (R. layout. collect_text, null );
}
Return txtEntryView;

} LayoutInflater can convert layout XML in the res folder to View. You can also use getLayoutInflater () or getSystemService (String) to create LayoutInflater.
[Java]
LayoutInflater inflater = (LayoutInflater) context. getSystemService
Context. LAYOUT_INFLATER_SERVICE );

LayoutInflater inflater = (LayoutInflater) context. getSystemService
Context. LAYOUT_INFLATER_SERVICE); inflate (int resId, ViewGroup root), convert the pre-defined XML layout file to View
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
 
<EditText xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/collected_text"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: padding = "5dp"/>

<? Xml version = "1.0" encoding = "UTF-8"?>

<EditText xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/collected_text"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: padding = "5dp"/>

 

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.