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)