Example 2 of AlertDialog in Android (normal option dialog box) and androidalertdialog

Source: Internet
Author: User

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}

 

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.