"Android Learning Notes" Custom Alertdialog dialog box

Source: Internet
Author: User


(1) Layout file:

<relativelayout 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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi        d:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:layout_marginleft= "70DP" android:layout_margintop= "62DP" android:text= "warning dialog box"/> <butt         On android:id= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"     android:layout_alignleft= "@+id/button1"   android:layout_below= "@+id/button1" android:layout_margintop= "64DP" android:text= "Multiple selection Dialog"/> <But ton android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:layout_alignleft=" @+id/button2 "android:layout_below=" @+id/button2 "Android:layout_marginto p= "60DP" android:text= "Single-selection dialog box"/></relativelayout>

(2) Type of file:

Package Com.example.alertdialog;import Android.os.bundle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.dialoginterface;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import android.widget.toast;/** * Warning dialog box using * * @author Piaodangdehun * */public class Mainactivity extends Activity {pri Vate button button1;//Warning Dialog Private button button2;//Multi-Select dialog box Private button button3;//Radio dialog Private Alertdialog.builder Bui Lder; dialog box for Button1, when there is a dialog box you can use the global definition @overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) This.findviewbyid (R.id.button1); Button2 = (Button) This.findviewbyid (r.id.button2) Button3 = (Button) This.findviewbyid (R.ID.BUTTON3);/* * 1. Warning dialog box Settings * /builder = new Alertdialog.builder (this); Builder.settitle ("cue box"); Builder.setmessage ("Are you sure you want to delete it?"). "); Builder.seticon (R.drawable.ic_launcher); Set Picture/* * OK dialog box */builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "Confirm Delete", 1). Show ();}); * * Cancel dialog box */builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "undelete", 1). Show ();}); * * Ignore dialog box */builder.setneutralbutton ("Ignore", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "Ignore delete", 1). Show ();}); Button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {builder.show ();}}); * * 2. Multi-Select dialog box * When there are multiple dialogs, no declaration definition */button2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) The {/* * dialog box was created */alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.settiTle ("Please choose Hobby"), final string[] hobby = {"Internet", "Play Game", "Chat"};/* * Selected list */builder.setmultichoiceitems (Hobby, new boolean[] { True, False,false},new Dialoginterface.onmultichoiceclicklistener () {@Overridepublic void OnClick (dialoginterface Dialog,int which, Boolean isChecked) {if (isChecked) {Toast.maketext (mainactivity.this, "-->>" + Hobby[which], 1). Show ();}}); * * Cancel button */builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog,int which) {}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@ overridepublic void OnClick (Dialoginterface dialog,int which) {}}); Builder.show ();}}); * * 3. Set */button3.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-gen) of the Radio dialog box Erated method Stubalertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.settitle ("XX"); Builder.setsinglechoiceitems (New string[]{"surfing", "Playing games", "chatting"},-1, new Dialoginterface.oncLicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}), Builder.setpositivebutton ("OK", New Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}); Builder.show ();}}); /* Private final View.onclicklistener Mylistenerclicklistener = new * Onclicklistener () {* * @Override public void OnC Lick (View v) {//TODO auto-generated method * stub *}}; */@Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is Present.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

(3) Results show



"Android Learning Notes" Custom Alertdialog dialog box

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.