Example 3 of AlertDialog in Android (one-way selection confirmation dialog box), androidalertdialog

Source: Internet
Author: User

Example 3 of AlertDialog in Android (one-way selection confirmation dialog box), 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. The following is a simple simulation of a selection confirmation dialog box (single choice) dialog box, different from the example 2 is that this time as long as you are not sure, you can regret it, such:

 

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 = "one-way selection" 7 android: layout_width = "match_parent" 8 android: layout_height = "wrap_content" 9 android: onClick = "show3" 10 android: id = "@ + id/button3"/> 11 </LinearLayout>

 

Java function implementation code:

1 public class AlertDialogDemo extends AppCompatActivity {2 int index = 0; 3 @ Override 4 protected void onCreate (@ Nullable Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. alertdialog); 7} 8 public void show3 (View v) {9 AlertDialog. builder builder = new AlertDialog. builder (this); 10 builder. setTitle ("pick a daughter"); 11 final String [] beautifulGirls = {"Sister Furong", "Sister Feng", "Fan Ye", "Myself"}; 12 builder. setSingleChoiceItems (beautifulGirls, 0, new DialogInterface. onClickListener () {13 @ Override14 public void onClick (DialogInterface dialog, int which) {15 index = which; 16} 17}); 18 builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {19 @ Override20 public void onClick (DialogInterface dialog, int which) {21 Toast. makeText (AlertDialogDemo. this, beautifulGirls [index] + "is your daughter-in-law", Toast. LENGTH_SHORT ). show (); 22} 23}); 24 builder. show (); 25} 26}

There is a lot of optimization space for everyone to play.

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.