(vii) Android Notification dialog box, and listen to the return key, when the return key is pressed to create a dialog box

Source: Internet
Author: User

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

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.