Believe that now Android software developers, are often used in this way pop-up dialog box. Android Inherits Dialogfragment Popup Dialog dialog box, so pop up has a lot of flexibility to handle the place, think what to start, when the data interface return can be, have self layout layout, life cycle. Here's a look at me writing the demo.
This article code download: please click here
Reprint Please specify source: http://blog.csdn.net/qq_16064871
First, mainactivity
Package Com.example.fragmentdialogdemo;import Com.example.fragmentdialogdemo.testdialog.ontestlistener;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Fragmentactivity implements Onclicklistener,ontestlistener {private String mstrname = "";p rivate String mstrhigh = ""; @Overrideprotected void Oncreat E (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitUI () ;} private void Initui () {button buttontest = (button) Findviewbyid (r.id.buttontest); Buttontest.setonclicklistener (This) ;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} The function of the interface callback @overridepublic void Ontestlistener (int uniqueidentifier, String strname,string strhigh) {if (UniqueidentiFier = = 1) {Toast.maketext (Getapplicationcontext (), "Name:" + strName + ", Height:" + strhigh, Toast.length_long). Show (); TextView textView1 = (TextView) Findviewbyid (r.id.textview1); Textview1.settext ("Name:" + strName + ", Height:" + strhigh);} Mstrname = Strname;mstrhigh = Strhigh;} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.buttontest://instantiation Testdialog, can be passed arguments, such as the title, or other parameters, there is a unique code. Testdialog dialog = new Testdialog (). newinstance ("Please enter", 1,mstrname, Mstrhigh);d ialog.show ( This.getsupportfragmentmanager (), "Testdialog"); break;default:break;}}}There are some comments, and sometimes it's important to watch me import the package.
Second, Testdialog
Package Com.example.fragmentdialogdemo;import Android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.support.v4.app.dialogfragment;import Android.view.view;import Android.widget.button;import Android.widget.edittext;public class Testdialog extends Dialogfragment {//Muniqueflag function is a unique code, can be made when the return to judge private int Muniqueflag = -1;private Ontestlistener monlistener;private EditText medittextname, medittexthigh;protected Button mbuttonpositive;/** * NEW instance * * @param title * @param unique * @param strName * @param strhigh * @return */public static T Estdialog newinstance (string title, int unique,string strName, string strhigh) {Testdialog tdialog = new Testdialog (); Bundle args = new Bundle (), args.putstring ("Selecttemplatetitle", title), Args.putint ("Multipletemplate", unique); Args.putstring ("templatename", StrName); Args.putstring ("Templatehigh", Strhigh); tdialog.setarguments (args); return tdialog;} PuBlic interface Ontestlistener {/** * * @param uniqueidentifier * Unique identification * @param strName * @param strhigh */PUBL IC abstract void Ontestlistener (int uniqueidentifier,string strName, String strhigh);} When rotated save @overridepublic void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); o Utstate.putstring ("InputName", Medittextname.gettext (). toString ()); Outstate.putstring ("Inputhigh", Medittexthigh.gettext (). toString ());} @Overridepublic Dialog Oncreatedialog (Bundle saveinstancestate) {String title = Getarguments (). GetString (" Selecttemplatetitle "); Muniqueflag = Getarguments (). GetInt (" Multipletemplate "); Alertdialog.builder Builder = new Alertdialog.builder (getactivity ()). Settitle (title). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//Trigger data callback if ( Monlistener = null) Monlistener.ontestlistener (Muniqueflag,medittextname.gettext (). toString (), Medittexthigh.gettext (). toString ());}). SetnegaTivebutton ("Cancel", null);//Add XML layout view view = Getactivity (). Getlayoutinflater (). Inflate (r.layout.test_dialog, NULL); Setupui (view);//after rotation, recover data if (saveinstancestate! = null) {String strName = saveinstancestate.getstring ("InputName"); if ( StrName = null) medittextname.settext (strName); String Strhigh = saveinstancestate.getstring ("Inputhigh"), if (strhigh! = null) Medittexthigh.settext (Strhigh);} Builder.setview (view);//Create dialog box Alertdialog Dialog = (alertdialog) builder.create (); return dialog;} private void Setupui (view view) {if (view = = null) return; String strName = getarguments (). getString ("templatename"); String Strhigh = getarguments (). getString ("Templatehigh"); medittextname = (EditText) View.findviewbyid ( R.id.edittextname); Medittexthigh = (EditText) View.findviewbyid (R.id.edittexthigh); Medittextname.settext (StrName) ; Medittexthigh.settext (Strhigh);} Onattach is associated with activity, with interface callback @overridepublic void Onattach (activity activity) {Super.onattach (activity); try { Monlistener = (ontestlistener) activity;} catch (ClassCastException e) {dismiss ();}}} There are many ways to inherit dialogfragment and the life cycle. Self-written can be more to see the method. This write is primarily data passing in, as well as data interface callbacks to activity.
Third, the activity of the XML
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= ". Mainactivity "> <textview android:id=" @+id/textview1 " android:layout_width=" Match_parent " android:layout_height= "80DP" android:gravity= "center" android:textsize= "18SP" android:text= "Click button"/> <button android:id= "@+id/buttontest" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "button"/></linearlayout>
Iv. Dialog dialog box XML
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= ". Mainactivity "> <textview android:id=" @+id/textview1 " android:layout_width=" Match_parent " android:layout_height= "80DP" android:gravity= "center" android:textsize= "18SP" android:text= "Click button"/> <button android:id= "@+id/buttontest" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "button"/></linearlayout>
Five, need to note, about the use of version issues
is primarily an issue with the SDK version, because fragment was proposed in 3.0, in order to be compatible with the lower version, a android-support-v4.jar was introduced, but when the Fragmentmanager was instantiated, This method cannot be used with Getfragmentmanager (). If this Getfragmentmanager () method is not found, it will be resolved as follows.
Workaround:
1. Introduction of a Android-support-v4.jar
2. Activity that needs to be instantiated must be extends fragmentactivity
3, with This.getsupportfragmentmanager (); to replace Getfragmentmanager ()
can also mainly have the above I write the example, also has this aspect reason. Note Import the jar package.
Six, the following
It's over, welcome to the exchange of learning.
This article code download: please click here
Reprint Please specify source: http://blog.csdn.net/qq_16064871
Android Inherits Dialogfragment Popup Dialog dialog box one