Android inheritance DialogFragment dialog box 2, androidfragment
Previously I wrote an article about Android inheritance DialogFragment pop-up dialog box 1. This time I modified some things based on the previous one, that is, how to get getDialog () in DialogFragment is to get the handle of the current dialog box. You can perform flexible operations with variable layout. It is used like getactivity. See the code below.
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 = ""; private String mstrHigh = ""; @ Overrideprotected void onCreate (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;} // interface callback function @ 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: // instantiate TestDialog. You can input parameters, such as the title or other parameters, and a unique code. testDialog dialog = new TestDialog (). newInstance ("enter", 1, mstrName, mstrHigh); dialog. show (this. getsuppfrfragmentmanager (), "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. onCheckedChangeLi Stener; public class TestDialog extends DialogFragment implements attributes {// mUniqueFlag is the unique code, which can be used to determine the private int mUniqueFlag =-1; private onTestListener mOnListener; private EditText meditTextName, meditTextHigh; protected Button mButtonPositive; /*** create an instance ** @ param title * @ param unique * @ param strName * @ param strHigh * @ return */public static TestDialog newInstance (String titl E, 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 Identifier * @ param StrName * @ param strHigh */public abstract void onTestListener (int uniqueIdentifier, String strName, String strHigh);} // save @ Overridepublic void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); outState. putString ("InputName", meditTextName. getText (). toString (); outState. putString ("InputHigh", meditTextHigh. getText (). toString () ;}@ Overridepublic Dialog onCreateDialog (Bun Dle saveInstanceState) {String title = getArguments (). getString ("SelectTemplateTitle"); mUniqueFlag = getArguments (). getInt ("MultipleTemplate"); AlertDialog. 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 the xml Layout View = getActivity (). getLayoutInflater (). inflate (R. layout. test_dialog, null); setupUI (view); // After rotation, restore the 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 the activity and calls back @ 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 (checkedId) {case R. id. radio0: // getDialog () is to get the 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: orientation = "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/radio1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "height"/> </RadioGroup> <LinearLayout android: id = "@ + id/LayoutName" android: layout_width = "match_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: layout_height =" wrap_content "android: layout_weight = "1" android: EMS = "10"> <requestFocus/> </EditText> </LinearLayout> <LinearLayout android: id = "@ + id/layouthweigh" android: layout_width = "match_parent" android: layout_height = "60dp"> <TextView android: layout_width = "wrap_content" android: layout_height = "match_parent" android: gravity = "left | blank" android: text = "height:" android: textSize = "18sp"/> <EditText android: id = "@ + id/editTextHigh" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: EMS = "10"> <requestFocus/> </EditText> </LinearLayout>
Ii. view the following figure for the effect
Iii. DialogFragment
There are many practices, summary, and communication in that sentence! We look forward to your message.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.