Example 2 of AlertDialog in Android (normal option dialog box) and androidalertdialog
In Android development, we often need to pop up some dialogs on the Android interface, such as asking users or asking users to choose. These functions are called the Android Dialog box. The AlertDialog implementation method is the builder mode. Next, we will simulate a simple and common option (single choice) dialog box for selecting huaki, for example:
Layout interface code:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <Button 6 android: text = "common options" 7 android: layout_width = "match_parent" 8 android: layout_height = "wrap_content" 9 android: onClick = "show2" 10 android: id = "@ + id/button2"/> 11 </LinearLayout>
Java function implementation code:
1 public class AlertDialogDemo extends AppCompatActivity {2 @ Override 3 protected void onCreate (@ Nullable Bundle savedInstanceState) {4 super. onCreate (savedInstanceState); 5 setContentView (R. layout. alertdialog); 6} 7 public void show2 (View v) {8 // instantiate builder 9 AlertDialog. builder builder = new AlertDialog. builder (this); 10 // set the title of the warning dialog box 11 builder. setTitle (""); 12 // define the warning box and select the content of the form (String array is used here) 13 final String [] beautifulGirls = {" ", "Fengjie", "Fan Ye", "Myself"}; 14 // set the elements in the array to each item in the dialog, and handle the event (use the toast here) 15 builder. setItems (beautifulGirls, new DialogInterface. onClickListener () {16 @ Override17 public void onClick (DialogInterface dialog, int which) {18 Toast. makeText (AlertDialogDemo. this, beautifulGirls [which] + "selected", Toast. LENGTH_SHORT ). show (); 19} 20}); 21 // display dialog box 22 builder. show (); 23} 24}