Android inheritance DialogFragment dialog box 1 and androiddialog

Source: Internet
Author: User

Android inheritance DialogFragment dialog box 1 and androiddialog

I believe that Android software developers often use this pop-up dialog box. Android inherits DialogFragment. the dialog box "dialog" is displayed, which can be used to flexibly handle the problem. You can choose what to start and when the data interface can be returned, and you can customize the layout and lifecycle. Let's take a look at my demo.

Download the code in this article: click here

Reprinted please indicate the source: http://blog.csdn.net/qq_16064871

1. 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 ;}}}
There are some annotations in it, and sometimes I need to focus on the import package.

Ii. 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 is a unique code that can be used to determine the priva when returned Te 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 title, int unique, string strName, String strHigh) {TestDialog tDialog = new TestDialog (); Bundle args = new Bundle (); args. putStri Ng ("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);} // when rotating @ Overridepublic void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); outState. 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 = 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; 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 ();}}}
Inheriting DialogFragment, there are many methods and lifecycles. You can view more methods for self-writing. The above code mainly refers to data input and data interface callback to activity.

Iii. xml of activity

<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 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>

5. Notes for using this version

The main problem is the sdk version, because Fragment was proposed in 3.0, in order to be compatible with the lower version, a android-support-v4.jar needs to be introduced, but when instantiating FragmentManager, The getFragmentManager () method cannot be used. If the getFragmentManager () method cannot be found, the solution is as follows.

Solution:

1. Introduce a android-support-v4.jar

2. The extends FragmentActivity must be instantiated.

3. Use this. getsuppfrfragmentmanager (); to replace getFragmentManager ()

The examples I wrote above can also be used for this reason. Import the jar package.

Vi. below


This is the end. welcome to join us.

Download the code in this article: click here

Reprinted please indicate the source: http://blog.csdn.net/qq_16064871


Related Article

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.