Alertdialog in Android (Warning dialog box)

Source: Internet
Author: User

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it Android Dialog dialog box, Alertdialog implementation method for Builder mode. Below we simulate the most common warning dialog box that pops up when uninstalling an application, such as:

Layout Interface code example:

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= "Unload" 7         android:layout_width= "match_parent" 8         android:layout_ height= "Wrap_content" 9         android:onclick= "show"         android:id= "@+id/button"/>  </LinearLayout>

Java implementation code:

1 ImportAndroid.content.DialogInterface;2 ImportAndroid.os.Bundle;3 Importandroid.support.annotation.Nullable;4 ImportAndroid.support.v7.app.AlertDialog;5 Importandroid.support.v7.app.AppCompatActivity;6 ImportAndroid.view.View;7 ImportAndroid.widget.Toast;8 /**9 * Created by Panchengjia on 2016/11/21.Ten  */ One  Public classAlertdialogdemoextendsappcompatactivity { A @Override -     protected voidonCreate (@Nullable Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.alterdialog); -     } -      Public voidShow (View v) { -         //instanced Builders +Alertdialog.builder Builder =NewAlertdialog.builder ( This); -         //set the caption of the warning dialog box +Builder.settitle ("Uninstall"); A         //set a picture for a warning display at //Builder.seticon (Android. R.drawable.ic_dialog_alert); -         //to Set Alert dialog box Information -Builder.setmessage ("OK uninstall?")); -         //set the "Front" button, and click events -Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { - @Override in              Public voidOnClick (Dialoginterface Dialog,intwhich) { -Toast.maketext (Alertdialogdemo. This, "clicked OK button", Toast.length_short). Show (); to             } +         }); -         //Set "Reverse" button, and click event theBuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { * @Override $              Public voidOnClick (Dialoginterface Dialog,intwhich) {Panax NotoginsengToast.maketext (Alertdialogdemo. This, "Clicked the Cancel button", Toast.length_short). Show (); -             } the         }); +         //set "neutral" button, and click event ABuilder.setneutralbutton ("Wait and see",NewDialoginterface.onclicklistener () { the @Override +              Public voidOnClick (Dialoginterface Dialog,intwhich) { -Toast.maketext (Alertdialogdemo. This, "clicked on the neutral button", Toast.length_short). Show (); $             } $         }); -         //Show dialog Box - builder.show (); the     } -}

Alertdialog in Android (Warning dialog box)

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.