Android DialogFragment dialog box

Source: Internet
Author: User

Android DialogFragment dialog box
Preface

Since I was engaged in Android development, the dialog box mentioned in Android Development reminds me of AlertDialog or PopupWindow. In my previous blog, I also summarized the usage of these two kinds of dialog boxes, interested in reliable look Android dialog box AlertDialog, PopupWindow usage Daquan. Since I recently summarized the usage of the Android dialog box, a new dialog box -- DialogFragment appeared after I saw Android3.0 on the Internet.

Significance of the DialogFragment dialog box

Why does the AlertDialog and PopupWindow dialog boxes in the android system basically meet the customer's needs? Why does it need to run a DialogFragment dialog box? This should begin with the advantages of DialogFragment:

The lifecycle is basically the same as that of Fragment, so it is easier for the Activity to better control and manage DialogFragment. The DialogFragment dialog box automatically adjusts the size of the dialog box as the screen is rotated (switching between portrait and portrait. AlertDialog and PopupWindow disappear with screen switching. The emergence of DialogFragment solves the problem of switching between the screen and the Dialog disappears. Basic usage dialog box of the DialogFragment dialog box

Sample Code:
Package com. xjp. dialogfragment; import android. app. fragment; import android. app. fragmentTransaction; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public voi D buttonClick (View view) {showDialog ();} private void showDialog () {/*** remove the displayed dialog box before the displayed dialog box to display the dialog without repeating the display. */FragmentTransaction ft = getFragmentManager (). beginTransaction (); Fragment fragment = getFragmentManager (). findFragmentByTag (basicDialog); if (null! = Fragment) {ft. remove (fragment);}/*** 0: Default style ** 1: No title style * 2: no border style * 3: Not input, do not obtain the focus style * dialog box that allows you to test these styles based on different parameters. */BasicDialogFragment dialogFragment = BasicDialogFragment. newInstace (0); dialogFragment. show (ft, basicDialog) ;}// DialogFragment code: package com. xjp. dialogfragment; import android. app. dialogFragment; import android. OS. bundle; import android. support. annotation. nullable; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. textView;/*** Descri Ption: Basic dialog box * User: xjp * Date: 2015/5/20 * Time: 8:44 */public class BasicDialogFragment extends DialogFragment {public static BasicDialogFragment newInstance (int style) {BasicDialogFragment dialogFragment = new BasicDialogFragment (); Bundle bundle = new Bundle (); bundle. putInt (style, style); dialogFragment. setArguments (bundle); return dialogFragment;} @ Override public void onCreate (Bundle sav EdInstanceState) {super. onCreate (savedInstanceState); int styleNum = getArguments (). getInt (style, 0); int style = 0; switch (styleNum) {case 0: style = DialogFragment. STYLE_NORMAL; // default style break; case 1: style = DialogFragment. STYLE_NO_TITLE; // No title style break; case 2: style = DialogFragment. STYLE_NO_FRAME; // borderless style break; case 3: style = DialogFragment. STYLE_NO_INPUT; // The focus style break cannot be obtained.} setStyle (st Yle, 0); // set the style} @ Nullable @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// getDialog (). setTitle (exit); // Add the title // getDialog (). requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title View = inflater. inflate (R. layout. fragent_basic_dialog, container); TextView title = (TextView) view. findViewById (R. id. title); title. setText (exit); TextView mes Sage = (TextView) view. findViewById (R. id. message); message. setText (whether to exit. After exiting, messages cannot be received. Whether to exit. After exiting, messages cannot be received. + Whether to exit. After exiting, messages cannot be received. Whether to exit. After exiting, messages cannot be received. + Whether to exit. After exiting, messages cannot be received .); View. findViewById (R. id. no ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {dismiss () ;}}); view. findViewById (R. id. yes ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {dismiss () ;}}); return view ;}// layout code:
  
      
       
        
    
   
  
Summary: In the basic dialog box, you only need to override the onCreateView method to load the dialog box. You can call setStyle (int, int) as needed to determine whether to modify the dialog box style, which has been implemented in the code above. It is worth noting that setStyle () must be set before the onCreateView () method is called. Therefore, the onStyle () method is generally called in the onCreate () method.How to cancel and set the title: setStyle (DialogFragment. STYLE_NO_TITLE, 0) and getDialog (). requestWindowFeature (Window. FEATURE_NO_TITLE). You can set the title to getDialog (). setTitle ("exit"); Use of the AlertDialog dialog box

Package com. xjp. dialogfragment; import android. app. fragment; import android. app. fragmentTransaction; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. widget. toast; public class MainActivity extends ActionBarActivity implements AlertDialogFragment. dialogFragmentClickImpl {@ Override protected void onCreate (Bundle savedInstanceState) {super. OnCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {showAlertDialog () ;}) ;} private void showAlertDialog () {FragmentTransaction ft = getFragmentManager (). beginTransaction (); Fragment fragment = getFragmentManager (). findFragmentByTag (basicDialog); if (null! = Fragment) {ft. remove (fragment);} AlertDialogFragment dialogFragment = new AlertDialogFragment (); dialogFragment. show (ft, basicDialog) ;}@ Override public void doPositiveClick () {showToast (OK button) ;}@ Override public void doNegativeClick () {showToast (cancel button );} private void showToast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show () ;}// DialogFragmentpackage com. xjp. dialogfragment; import android. app. alertDialog; import android. app. dialog; import android. app. dialogFragment; import android. content. dialogInterface; import android. OS. bundle; import android. util. log;/*** Description: * User: xjp * Date: 2015/5/20 * Time: 11: 18 */public class extends DialogFragment {public interface DialogFragmentClickImpl {void doPositiveClick (); void doNegativeClick () ;}@ Override public Dialog onCreateDialog (Bundle savedInstanceState) {Log. e (TAG, ** onCreateDialog **); return new AlertDialog. builder (getActivity ()). setIcon (R. drawable. ic_launcher ). setTitle (exit ). setMessage (whether to exit, whether to exit ,). setPositiveButton (android. r. string. OK, new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int whichButton) {DialogFragmentClickImpl impl = (DialogFragmentClickImpl) getActivity (); impl. doPositiveClick ();}}). setNegativeButton (android. r. string. cancel, new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int whichButton) {DialogFragmentClickImpl impl = (DialogFragmentClickImpl) getActivity (); impl. doNegativeClick ();}}). create ();}}
Summary:

1. You can load the dialog box in onCreateView or override onCreateDialog. Of course, the two do not appear at the same time.
2. Communication between the dialog box and the Activity can be achieved through the interface, the DialogFragmentClickImpl interface of the code above.
3. onCreateView can load a custom dialog box. The onCreateDialog system AlertDialog Type dialog box is suitable.
4. DialogFragment only implements a Dialog in Fragment.
5. the dialog box is not closed when the DialogFragmnet dialog box is landscape. Because the DailogFragment has the Fragment attribute, the DialogFragment is re-created when the screen changes. See the figure below:

 

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.