First,the code in Activity_main.java
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:gravity= "Center"6tools:context= "Com.example.Dialog.MainActivity" >7<Button8android:onclick= "click"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "dialog box"/> A</LinearLayout>
second, the code in the Mainactivity.java
1 PackageCom.example.Dialog;2 3 Importandroid.app.Activity;4 ImportAndroid.app.AlertDialog;5 ImportAndroid.app.AlertDialog.Builder;6 ImportAndroid.app.Dialog;7 ImportAndroid.content.DialogInterface;8 ImportAndroid.content.DialogInterface.OnClickListener;9 ImportAndroid.os.Bundle;Ten Importandroid.view.KeyEvent; One ImportAndroid.view.View; A ImportAndroid.widget.Toast; - - Public classMainactivityextendsActivity { the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - } + A Public voidClick (View view) { atAlertdialog.builder Builder =NewBuilder ( This); -Builder.settitle ("Hint"); - Builder.seticon (r.drawable.ic_launcher); -Builder.setmessage ("Do you want to make a software upgrade"); - //builder.setcancelable (false); -Builder.setpositivebutton ("OK",NewOnclicklistener () { in @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { to //TODO auto-generated Method Stub +Toast.maketext (mainactivity. This, "OK was clicked", 0). Show (); - } the }); *Builder.setnegativebutton ("Cancel",NewOnclicklistener () { $ Panax Notoginseng @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { the //TODO auto-generated Method Stub +Toast.maketext (mainactivity. This, "Canceled was clicked", 0). Show (); A } the }); + builder.create (). Show (); - } $ $ @Override - Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { - //TODO auto-generated Method Stub the - if(KeyCode = =keyevent.keycode_back) {WuyiAlertdialog.builder Builder =NewBuilder ( This); theBuilder.settitle ("Hint"); -Builder.setmessage ("OK to exit"?)); Wu builder.create (); - Dialog Dialog; About $Builder.setpositivebutton ("OK",NewOnclicklistener () { - - @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { A //TODO auto-generated Method Stub + Dialog.dismiss (); the } - }); $Builder.setnegativebutton ("Cancel",NewOnclicklistener () { the the @Override the Public voidOnClick (Dialoginterface Dialog,intwhich) { the //TODO auto-generated Method Stub - in } the }); theDialog =builder.show (); About the } the return false; the } +}
(vii) Android Notification dialog box, and listen to the return key, when the return key is pressed to create a dialog box