Android -- Dialog box
In Android development, a Dialog box is displayed on the interface, so that we often need to do this. In this article, we will make a record for querying when we forget it.
Dialog is a dialog box on the screen that allows you to make a choice or enter additional information. A dialog box is not covered by our entire screen, in addition, it is usually used in model events to continue execution only after the user makes a decision.
The Dialog class is the base class of the dialog box, but we should avoid directly using this class to instantiate a Dialog dialog box. We should use its subclass to get a dialog box:
java.lang.Object android.app.DialogKnown Direct SubclassesAlertDialog, CharacterPickerDialog, MediaRouteChooserDialog, MediaRouteControllerDialog, PresentationKnown Indirect SubclassesDatePickerDialog, ProgressDialog, TimePickerDialog
We can see that Dialog has many subclass implementations, so we need to define a Dialog box and use its subclass to instantiate one, instead of directly constructing it using the parent class Dialog.
We can see that AlertDialog is a direct subclass of Dialog.
With AlertDialog, We can display a title, up to three button operations, a set of selection boxes, or custom pop-up boxes.
In AlertDialog, the defined Buttons are all completed through the setXXXButton method. There are three different actions Buttons for us to choose from:
1. setPositiveButton (CharSequence text, DialogInterface. onClickListener listener) This is a button equivalent to OK and OK operations, 2. setNegativeButton (CharSequence text, DialogInterface. onClickListener listener) is a button equivalent to canceling an operation. 3. setNeutralButton (CharSequence text, DialogInterface. OnClickListener listener) is equivalent to a button that ignores the operation.
Each of our actions, buttons, can appear at most one, that is, a pop-up dialog box can only have one PositiveButton.
Next, let's take a look at several common AlertDialog dialogs through a specific instance.
1. A warning box is displayed, and three buttons are available.
Let's take a look at the Code:
Button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// use alertdiener. builder class to instantiate an AlertDialog object AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); // set the Title icon builder. setIcon (R. drawable. ic_launcher); // set the Title content builder. setTitle ("pop-up warning box"); // sets the Content to display an information builder. setMessage ("are you sure you want to delete it? "); // Set a PositiveButton builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "positive:" + which, Toast. LENGTH_SHORT ). show () ;}}); // sets a NegativeButton builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "negative:" + which, Toast. LENGTH_SHORT ). show () ;}}); // sets a NeutralButton builder. setNeutralButton ("Ignore", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "neutral:" + which, Toast. LENGTH_SHORT ). show () ;}}); // display the builder of the dialog box. show ();}});
If you want to create an AlertDialog dialog box, you must use an internal class of AlertDialog, that is, AlertDialog. Builder, to create an AlertDialog dialog box. Then, you can set the content you want to display using the setXX method.
We can see that we have set three actions in total, and each button is bound with a DialogInterface. onClickListener () listens to events, and then uses the Toast dialog box (which will be explained later) to pop up some of our information, the which method indicates the int value represented by action button:
positive: -1negative: -2neutral: -3
We can know that which =-1 indicates that the OK button is clicked,-2 indicates that the cancel button is clicked, and-3 indicates that the ignore button is clicked.
Import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. OS. handler; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class SingleChoiceItemsTest extends Activity implements OnClickListener {private String [] province = new String [] {"Shanghai ", "Beijing", "Hunan", "Hubei", "Hainan"}; private Button btnSingleChoiceList; // click the instance of the event object private ButtonOnClick buttonOnClick = new ButtonOnClick (1 ); /** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); btnSingleChoiceList = (Button) findViewById (R. id. btnSingleChoice); btnSingleChoiceList. SetOnClickListener (this) ;}@ Override public void onClick (View v) {showSingleChoiceButton ();} // display the OK and cancel buttons in the single-choice options. // the Data Type of the buttonOnClickg variable is ButtonOnClick. One click event class private void showSingleChoiceButton () {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("select Province"); builder. setSingleChoiceItems (province, 0, buttonOnClick); builder. setPositiveButton ("OK", buttonOnClick); buil Der. setNegativeButton ("cancel", buttonOnClick); builder. show ();} private class ButtonOnClick implements DialogInterface. onClickListener {private int index; // indicates the option index public ButtonOnClick (int index) {this. index = index ;}@ Override public void onClick (DialogInterface dialog, int which) {// which indicates the clicked button index. All option indexes are greater than 0, button indexes are all smaller than 0. If (which> = 0) {// if you click a list item, save the index of the current list item in the index. // To close the dialog box after clicking a list item, call dialog. cancel () // or use the dialog. dismiss () method. Index = which;} else {// the user clicks the [OK] button if (which = DialogInterface. BUTTON_POSITIVE) {// displays the list items selected by the user. Final AlertDialog ad = new AlertDialog. builder (SingleChoiceItemsTest. this ). setMessage ("the region you selected is:" + index + ":" + province [index]). show (); // It is automatically disabled in five seconds. Handler hander = new Handler (); Runnable runnable = new Runnable () {@ Override public void run () {ad. dismiss () ;}}; hander. postDelayed (runnable, 5*1000);} // the user clicks the cancel button else if (which = DialogInterface. BUTTON_NEGATIVE) {Toast. makeText (SingleChoiceItemsTest. this, "You have not selected anything", Toast. LENGTH_LONG );}}}}}
A drop-down ticket is displayed.
Button3.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("select gender"); final String [] sex = {"male", "female", "unknown gender "}; // set a single selection drop-down box/*** the first parameter specifies the data set of the drop-down list to be displayed * The second parameter indicates the index, specify which single listener is selected by default. 1 indicates that the default 'femal' is checked. * The third parameter is used to bind a listener to each single option */builder. setSingleChoiceItems (sex, 1, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "Gender:" + sex [which], Toast. LENGTH_SHORT ). show () ;}}); builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); builder. show ();}});Note: When you select an option in the drop-down list, the dialog box will not disappear. You need to click action button to make the dialog box disappear.
A drop-down and multi-choice box is displayed.
Button4.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("hobbies"); final String [] hobbies = {"basketball", "football", "tennis", "Snooker "}; // set a single selection drop-down box/** the first parameter specifies the data set of the drop-down and multiple selection boxes to be displayed * The second parameter indicates which options are selected, if it is null, it indicates that no one is selected. If you want to specify which option box is selected, * You need to pass a boolean [] array, the length must be the same as that of the first parameter, for example, {true, false, false, true}. * The third parameter binds a listener to each multiple option */builder. setMultiChoiceItems (hobbies, null, new DialogInterface. onMultiChoiceClickListener () {StringBuffer sb = new StringBuffer (100); @ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {if (isChecked) {sb. append (hobbies [which] + ",");} Toast. makeText (MainActivity. this, "Hobbies:" + sb. toString (), Toast. LENGTH_SHORT ). show () ;}}); builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); builder. show ();}});
Similarly, if you select one of the multiple drop-down boxes, the dialog box does not disappear and only the action button is clicked.