1 PackageCOM.WHJ.NTWB;2 3 ImportAndroid.annotation.SuppressLint;4 Importandroid.app.Activity;5 ImportAndroid.app.AlertDialog;6 ImportAndroid.app.Dialog;7 Importandroid.app.DialogFragment;8 ImportAndroid.content.DialogInterface;9 ImportAndroid.os.Bundle;Ten ImportAndroid.util.Log; One ImportAndroid.view.View; A ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; - ImportAndroid.widget.TextView; the -@SuppressLint ("Newapi") Public classMainactivityextendsactivity{ - @Override - protected voidonCreate (Bundle savedinstancestate) { + //TODO auto-generated Method Stub - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.layout_linearlayout); AView TV =Findviewbyid (r.id.text); at((TEXTVIEW) TV). SetText ("Example of displaying an alert dialog with a dialogfragment"); - - //Watch for button clicks. -Button Button =(Button) Findviewbyid (r.id.show); -Button.setonclicklistener (NewOnclicklistener () { - Public voidOnClick (View v) { in ShowDialog (); - } to }); + } - the voidShowDialog () { *Dialogfragment newfragment =Myalertdialogfragment.newinstance ( $ r.string.alert_dialog_two_buttons_title);Panax NotoginsengNewfragment.show (Getfragmentmanager (), "dialog"); - } the + Public voidDopositiveclick () { A //Do stuff here. theLOG.I ("Fragmentalertdialog", "Positive click!"); + } - $ Public voidDonegativeclick () { $ //Do stuff here. -LOG.I ("Fragmentalertdialog", "negative click!"); - } the - Wuyi the Public Static classMyalertdialogfragmentextendsdialogfragment { - Wu Public StaticMyalertdialogfragment newinstance (inttitle) { -Myalertdialogfragment Frag =Newmyalertdialogfragment (); AboutBundle args =NewBundle (); $Args.putint ("title", title); - frag.setarguments (args); - returnFrag; - } A + @Override the PublicDialog Oncreatedialog (Bundle savedinstancestate) { - inttitle = Getarguments (). GetInt ("title"); $ the return NewAlertdialog.builder (getactivity ()) the . SetIcon (R.drawable.ic_launcher) the . Settitle (title) the. Setmessage ("ddddddddddd") - . Setpositivebutton (R.STRING.ALERT_DIALOG_OK, in NewDialoginterface.onclicklistener () { the Public voidOnClick (Dialoginterface Dialog,intWhichbutton) { the ( (mainactivity) getactivity ()). Dopositiveclick (); About } the } the ) the . Setnegativebutton (R.string.alert_dialog_cancel, + NewDialoginterface.onclicklistener () { - Public voidOnClick (Dialoginterface Dialog,intWhichbutton) { the ( (mainactivity) getactivity ()). Donegativeclick ();Bayi } the } the ) - . Create (); - } the } the}
Use of Dialogfragment