Previously wrote an article about Android Inheritance Dialogfragment Popup Dialog dialog one, this time on the basis of the last modification of some things, is how to get Getdialog in Dialogfragment () is to get the current dialog box handle. You can perform flexible operations with variable layouts. Just like getactivity (); use. See the code below.
This demo: click
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;}}}
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;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;public class Testdialog extends Dialogfragment implements oncheckedchangelistener{//Muniqueflag function is a unique code that can make the return of the judgment private int muniqueflag = -1;private Ontestlistener Monlistener ;p rivate EditText medittextname, medittexthigh;protected Button mbuttonpositive;/** * NEW instance * * @param title * @param uniq UE * @param strName * @param strhigh * @return */public static testdialog newinstance (String title, int unique,string StrN Ame, String strhigh) {testdialog tdialog = new Testdialog (); Bundle args = new Bundle (), args.putstring ("Selecttemplatetitle", title), Args.putint ("Multipletemplate", unique);Rgs.putstring ("templatename", StrName); Args.putstring ("Templatehigh", Strhigh); tdialog.setarguments (args); return Tdialog;} Public interface Ontestlistener {/** * * @param uniqueidentifier * Unique identification * @param strName * @param strhigh */pu Blic 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; Radiogroup radioGroup1 = (radiogroup) View.findviewbyid (r.id.radiogroup1); Radiogroup1.setoncheckedchangelistener ( this); 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 ();}} @Overridepublic void OnCheckedChanged (radiogroup group, int checkedid) {if (Group.getid () = = r.id.radiogroup1) {switch (c Heckedid) {case R.id.radio0://getdialog () is the Get Current dialog box Getdialog (). Findviewbyid (R.id.layoutname). Setvisibility ( view.visible); Getdialog (). Findviewbyid (R.id.layouthigh). setvisibility (view.visible); Break;case R.id.radio1: Getdialog (). Findviewbyid (R.id.layoutname). setvisibility (View.gone); Getdialog (). Findviewbyid (R.id.LayoutHigh). Setvisibility (view.visible); break;default:break;}}}
Activity_main
<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>
Test_dialog
<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:orien tation= "vertical" > <radiogroup android:id= "@+id/radiogroup1" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <radiobutton Android:id= "@+id/radio0" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:checked= "true" android:text= "name"/> <radiobutton android:id= "@+id/radi O1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:text = "height"/> </RadioGroup> <linearlayout android:id= "@+id/layoutname" Android:layout_width= "Ma Tch_parent "android:layout_height=" 60DP " > <textview android:layout_width= "wrap_content" android:layout_height= "Match_parent" Android:gravity= "left|center_vertical" android:text= "Name:" android:textsize= "18sp"/> <edittext android:id= "@+id/edittextname" android:layout_width= "Wrap_content" Android oid:layout_height= "Wrap_content" android:layout_weight= "1" android:ems= "ten" > <requ Estfocus/> </EditText> </LinearLayout> <linearlayout android:id= "@+id/layouthigh" Android:layout_width= "Match_parent" android:layout_height= "60DP" > <textview Android : layout_width= "wrap_content" android:layout_height= "match_parent" android:gravity= "Left|center_verti Cal "android:text=" Height: "android:textsize=" 18sp "/> <edittext android:id=" @+i D/edittexthigh " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1 "android:ems=" > <requestfocus/> </EditText> </LinearLayout>< /linearlayout>
second, the effect of the following dynamic view
Part III Methods of Dialogfragment
Or that sentence more practice, more summary, more exchanges! Look forward to your message.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Inheritance Dialogfragment Popup Dialog dialog Box II