Android dialogfragment dialog box

Source: Internet
Author: User

Write in front

Engaged in Android development so far, a reference to the use of Android Development dialog box to think of Alertdialog or Popupwindow, I also summarized in the previous blog The use of the two dialog box, interested in the reliable look look Android dialog box Alertdialog,popupwindow use Daquan. As a result of the recent summary of the use of Android dialog box, I have no intention to see Android3.0 on the Internet after a new dialog box--dialogfragment appears.

The meaning of the Dialogfragment dialog box appears

Why does the Android system have a Alertdialog,popupwindow dialog, basically to meet the needs of customers, why also run out a dialogfragment dialog box? This is going to start with the advantages of dialogfragment:

    1. There is a basic consistent life cycle with fragment, so it is easier for the activity to control management dialogfragment better.
    2. The Dialogfragment dialog box automatically adjusts the size of the dialog box as it rotates with the screen (screen toggle). The Alertdialog and Popupwindow disappear with the screen switch.
    3. The appearance of Dialogfragment solves the problem that screen switching dialog disappear.
Dialogfragment dialog box using the Basic dialog

code example:
 PackageCom.xjp.dialogfragment;ImportAndroid.app.Fragment;ImportAndroid.app.FragmentTransaction;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.View; Public classMainactivity extends Actionbaractivity {@Override    protected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_main); } Public voidButtonClick (view view) {ShowDialog (); }Private voidShowDialog () {/** * To not display the dialog repeatedly, remove the dialog box that is being displayed before the dialog box is displayed. */Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction (); Fragment Fragment = Getfragmentmanager (). Findfragmentbytag ("Basicdialog");if(NULL! = fragment) {Ft.remove (fragment); }/** * 0: Default Style * 1: Untitled style * 2: no Border Style * 3: No input, no focus style * You can perform a test of these types of dialog boxes depending on the parameters. */Basicdialogfragment dialogfragment = Basicdialogfragment.newinstace (0); Dialogfragment.show (FT,"Basicdialog"); }}//dialogfragment Code: PackageCom.xjp.dialogfragment;ImportAndroid.app.DialogFragment;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;/** * Description: Basic dialog box * USER:XJP * DATE:2015/5/20 * time:8:44 * * Public classBasicdialogfragment extends Dialogfragment { Public StaticBasicdialogfragment newinstance (intStyle) {Basicdialogfragment dialogfragment =NewBasicdialogfragment (); Bundle bundle =NewBundle (); Bundle.putint ("Style", style); Dialogfragment.setarguments (bundle);returnDialogfragment; }@Override     Public voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);intStylenum = Getarguments (). GetInt ("Style",0);intstyle =0;Switch(Stylenum) { Case 0: style = Dialogfragment.style_normal;//Default style                 Break; Case 1: style = Dialogfragment.style_no_title;//Untitled style                 Break; Case 2: style = Dialogfragment.style_no_frame;//No Border style                 Break; Case 3: style = Dialogfragment.style_no_input;//No input, no focus style available                 Break; } SetStyle (Style,0);//Set style}@Nullable    @Override     PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//Getdialog (). Settitle ("exit");//Add title//Getdialog (). Requestwindowfeature (Window.feature_no_title);//Remove titleView view = Inflater.inflate (R.layout.fragent_basic_dialog, container);        TextView title = (TextView) View.findviewbyid (r.id.title); Title.settext ("Exit");        TextView message = (TextView) View.findviewbyid (r.id.message); Message.settext ("If you exit, you cannot receive messages after exiting. Whether to exit and not receive messages after exiting. "+"If you exit, you cannot receive messages after exiting. Whether to exit and not receive messages after exiting. "+"If you exit, you cannot receive messages after exiting. "); View.findviewbyid (r.id.no). Setonclicklistener (NewView.onclicklistener () {@Override             Public voidOnClick (View v) {dismiss ();        }        }); View.findviewbyid (R.id.yes). Setonclicklistener (NewView.onclicklistener () {@Override             Public voidOnClick (View v) {dismiss (); }        });returnView }}//Layout code:<?xmlversion="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:orientation="Vertical"> <textview android:id="@+id/title"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_gravity="Center"android:text="Basic dialog box"Android:textcolor="#ff0000"/> <view android:layout_margintop="5DP"Android:layout_width="Match_parent"android:layout_height="2DP"Android:background="#ff009688"></View> <textview android:id="@+id/message"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_gravity="Center"android:layout_margintop="10DP"android:text="This is a basic dialog box test case, this is a basic dialog box test case, this is a basic dialog box test case, this is a basic dialog box test case, This is a basic dialog box test case,"/></linearlayout>
Summarize:
    1. The basic dialog box simply overrides the Oncreateview method to load the dialog box.
    2. You can call SetStyle (int,int) To modify the dialog box style according to your own needs, which is already implemented in the code above. It is worth noting that setting SetStyle () must be valid before the Oncreateview () method call, so the Onstyle () method is typically called in the OnCreate () method.
    3. Methods for canceling and setting headers: SetStyle (dialogfragment.style_no_title,0) and Getdialog (). Requestwindowfeature (Window.feature_no_title) , the set caption can be Getdialog (). Settitle ("Exit");
Alertdialog Use of the dialog box

 PackageCom.xjp.dialogfragment;ImportAndroid.app.Fragment;ImportAndroid.app.FragmentTransaction;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.View;ImportAndroid.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 (NewView.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 =NewAlertdialogfragment (); 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 (); }}//dialogfragment PackageCom.xjp.dialogfragment;ImportAndroid.app.AlertDialog;ImportAndroid.app.Dialog;ImportAndroid.app.DialogFragment;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.util.Log;/** * Description: * USER:XJP * DATE:2015/5/20 * time:11:18 * * Public  class alertdialogfragment extends dialogfragment {     Public  interface dialogfragmentclickimpl {        voidDopositiveclick ();voidDonegativeclick (); }@Override     PublicDialogOncreatedialog(Bundle savedinstancestate) {LOG.E ("TAG","**oncreatedialog**");return NewAlertdialog.builder (Getactivity ()). SetIcon (R.drawable.ic_launcher). Settitle ("Exit"). Setmessage ("Whether to exit, whether to exit, whether to exit, whether to exit,"). Setpositivebutton (Android. R.string.ok,NewDialoginterface.onclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {Dialogfragmentclickimpl Impl = (Dialogfragmentclickimpl) getactivity ();                            Impl.dopositiveclick (); }}). Setnegativebutton (Android. R.string.cancel,NewDialoginterface.onclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {Dialogfragmentclickimpl Impl = (Dialogfragmentclickimpl) getactivity ();                            Impl.donegativeclick ();    }}). Create (); }}
Summarize:

1. You can load a dialog box in Oncreateview, or you can rewrite Oncreatedialog, but not both.
2. The communication between the dialog and activity can be implemented by means of an interface, such as the Dialogfragmentclickimpl interface of the above code.
3.onCreateView can load customized dialog box, Oncreatedialog Loading System alertdialog Type dialog box is more appropriate.
4.DialogFragment just implemented a dialog in fragment.
The dialog box does not close when the 5.DialogFragmnet dialog is horizontal, because Dailogfragment has the Fragment property, which re-creates the dialogfragment when the screen changes. There is a picture for proof:

SOURCE download

Android dialogfragment dialog box

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.