The dialog box is a pop-up window in which the program is running. The Android system provides four types of dialogs: The alarm dialog box (AlertDialg), the Progress dialog box (ProgressDialog), the date selection dialog box (DatePickerDialog), and the time dialog box (TimePickerDialog ). The AlertDialog dialog box is described in subsequent chapters.
AlertDialog is a prompt window that requires you to make a selection. The dialog box contains several selection buttons, title information, and prompt information.
To create a dialog box in a program, follow these steps:
1. Obtain the static internal class Builder object of AlertDialog, which creates a dialog box.
2. Set the title, button, and event to be responded to by using the Builder object.
3. Call the create () method creation dialog box of Builder.
4. Call the show () method of AlertDialog to display the dialog box.
Case 1: a dialog box with "yes", "no" buttons and prompt information is created below to confirm the user's operations.
Package com. xiaomo. ui; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import com. example. helloworld. r; public class ALert_Dialog extends Activity {// declare the TextView object private TextView myTextView; // declare the Button Object private Button myButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); myTextView = (TextView) findViewById (R. id. main_text); myButton = (Button) findViewById (R. id. main_button); // instantiate AlertDailog. builder object final AlertDialog. builder builder = new AlertDialog. builder (this); // Click Event myButton for the Add button. setOnClickListener (ne W OnClickListener () {public void onClick (View v) {// set the display information builder. setMessage ("are you sure you want to delete this record? "). // SetPositiveButton ("yes", new DialogInterface. onClickListener () {// Click Event public void onClick (DialogInterface dialog, int which) {// set TextView text myTextView. setText ("deleted successfully! ");}}). // Set the cancel button setNegativeButton ("no", new DialogInterface. onClickListener () {public void onClick (DialogInterface dialog, int which) {myTextView. setText ("cancel deletion! ") ;}}); // Create dialog box AlertDialog ad = builder. create (); // display the dialog box ad. show ();}});}}
Case 2: A context menu is created to set multiple entry options.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> package com. xiaomo. ui; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import com. example. helloworld. r; public class Context_Alert_Dialog extends Activity {// declare the component private TextView myTextView; private Button myButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the component instance myTextView = (TextView) findViewById (R. id. main_text); myButton = (Button) findViewById (R. id. main_button); // array of options in the dialog box final String [] items = {"Milk Tea", "coffee", "juice"}; // instantiate AlertDialog. builder instance final AlertDialog. builder builder = new AlertDialog. builder (this); // Add button click listener myButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {// set the title and option builder. setTitle ("order "). setItems (items, new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int which) {// set the text content to the selected option myTextView. setText (items [which]) ;}}); // display it in a single button style // builder. setTitle ("order "). setSingleChoiceItems (items,-1, // new DialogInterface. onClickListener () {// public void onClick (DialogInterface dialog, // int which) {// myTextView. setText (items [which]); //}); AlertDialog ad = builder. create (); ad. show ();}});}}
Source: http://blog.csdn.net/cl05300629/article/details/17962643 Author: Standing