Android Create dialog box (OK Cancel dialog box, Radio dialog box, multi-selection dialog box) instance code _android

Source: Internet
Author: User
Tags event listener

Android can create three kinds of dialog boxes, OK Cancel dialog box, Radio dialog box, multiple selection dialog box

The OK Cancel dialog box Demo example in Android


Demo case using a Radio dialog box in Android


Demo case with multiple-selection dialog box in Android


The implementation code is as follows

modifying Activity_main.xml files

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
xmlns:tools=" Http://schemas.android.com/tools " 
android:layout_width=" Match_parent 
" android:layout_height= "Match_parent" 
tools:context= "com.fyt.dialogdemo.MainActivity" 
android:o rientation= "vertical" > 
<button 
android:layout_width= "Wrap_content" 
Wrap_content " 
android:text=" OK Cancel dialog box " 
android:onclick=" Clicked1/> 
<button 
android: Layout_width= "Wrap_content" 
android:layout_height= "wrap_content" 
android:text= "Radio dialog box" 
android:o nclick= "Clicked2"/> 
<button 
android:layout_width= "Wrap_content" 
Wrap_content " 
android:text=" multiple-selection dialog 
android:onclick= "Clicked3"/> 
</LinearLayout>

Finally modify the code in the Mainactivity.java

Package Com.fyt.dialogdemo; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.app.AlertDialog; 
Import Android.app.AlertDialog.Builder; 
Import Android.content.DialogInterface; 
Import Android.content.DialogInterface.OnClickListener; 
Import Android.content.DialogInterface.OnMultiChoiceClickListener; 
Import Android.view.View; 
Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.oncre 
Ate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
}//OK Cancel Dialog button response function public void Clicked1 (view view) {//Create a Radio dialog box using the Creator Alertdialog.builder Builder = new Builder (this); Set the icon for the dialog box Builder.seticon (Android. 
R.drawable.alert_light_frame); 
Set the title of the dialog box Builder.settitle ("to practice this work must first from the palace"); 
Set the text Builder.setmessage ("林平, Think clearly oh"); Set the OK button Builder.setpositivebutton (OK), new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (MaiNactivity.this, "Thanks for using the software, Goodbye", Toast.length_short). Show (); 
} 
}); Set Cancel button Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, 
int which) {Toast.maketext (mainactivity.this, "if not from the palace, must not be successful", Toast.length_short). Show (); 
} 
}); 
Generates a dialog object using the Creator Alertdialog ad = Builder.create (); 
Ad.show (); 
}//Radio dialog Box button response function public void Clicked2 (view view) {Alertdialog.builder Builder = new Builder (this); 
Set the title of the Builder.settitle dialog box ("Please select Gender"); 
Create an array of strings to set the entries in a radio dialog final string[] items = {"Male", "female"}; 
Create a Radio dialog box//First parameter: The string array of entries displayed in the Radio dialog box//second parameter: The subscript of the default selected entry (-1 indicates that no entry is selected by default)//third parameter: Set event listener Builder.setsinglechoiceitems (items,-1, New Onclicklistener () {//which: subscript//dialog for the user-Selected entry: dialog box that triggers this method @Override Publi  c void OnClick (Dialoginterface dialog, int which) {//Use the Toast dialog box to prompt the user to select the sex Toast.maketext (Mainactivity.this, "Your Chosen sex is:" + 
Items[which], Toast.length_short). Show (); 
Closes the dialog box Dialog.dismiss (); 
} 
}); Display a Radio dialog box builder.show(); 
}//Multiple-selection dialog box button response function public void Clicked3 (view view) {Alertdialog.builder Builder = new Builder (this); 
Set the title of the Builder.settitle dialog box ("Please select the person you think is handsome"); 
Creates an array of strings to set options in the Multiple-selection dialog box Final string[] items = {"Kan elder brother", "Zhao Shuai elder brother", "Zhao Teacher", "Zhao Shi brother"}; 
Creates a Boolean array that sets whether entries in the Multiple-selection dialog box are selected for final boolean[] CheckedItems = {false, False, False, false}; Create a multiple-selection dialog box//First argument: The string array of entries in the Multiple-selection dialog box//second parameter: the array where the entry is checked or not the third parameter: Create Event listener builder.setmultichoiceitems (items, CheckedItems, New Onmultichoiceclicklistener () {//which: User clicks the subscript of the entry//ischecked: whether the user selects the entry or cancels the entry @Override public void 
OnClick (dialoginterface dialog, int which, Boolean ischecked) {Checkeditems[which] = ischecked; 
} 
}); Set a OK button Builder.setpositivebutton (OK), new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog 
, int which) {String text = ""; 
Log the user-selected entries in the Multiple-selection dialog box for (int i = 0; i < 4; i++) {text + checkeditems[i]? Items[i] + ",": ""; Use the Toast dialog box to display user-selected entries Toast.maketext (mainactivity.this, TEXT, 0). Show (); 
Hide multiple-selection dialog box Dialog.dismiss (); 
} 
}); 
Display multiple-selection dialog box Builder.show ();  } 
}

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.