The Dialogfragment of Android learning

Source: Internet
Author: User

Dialogframent is the base class for dialog boxes in the Android SDK that developers can extend for this class. He can expand a variety of dialog boxes in Android, such as Alertdialog,listdialog,radiodialog. Android has provided a simple dialog box for developers, and this article is about the Extended section of the dialog box. This example is based on the SDK development documentation.

1. Establish Textdialog

Under the Layout folder, create a textdialog XML document. In the XML document, we have defined only one textview.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6     7 <TextView8     Android:id= "@+id/text"9 Android:layout_width= "Fill_parent"Ten Android:layout_height= "Wrap_content" /> One  A </LinearLayout>
Textdialog

2. Establishing the Textdialog class

Adding a new class textdialogfragment in SRC, the class needs to inherit dialogfragment and need to implement some of its methods, such as OnCreate (), Oncreateview (), and so on. In the class, the primary is to set the fields that need to be displayed for TextView, and to set the styles and Themes dialog display. Here is the main part of the dialogfragment extension, where we can define our own methods, define events, and so on. Rather than poetry listdialog, you need to define an adapter, and you need to define ItemClick.

1  Public classTextdialogframnetextendsdialogfragment {2     3     intMnum;4 5     StaticTextdialogframnet newinstance (intnum)6     {7Textdialogframnet textdia=Newtextdialogframnet ();8Bundle bundel=NewBundle ();9Bundel.putint ("name", num);Ten textdia.setarguments (Bundel); One         returnTextdia; A     } -      Public voidonCreate (Bundle saveinstanced) -     { the         Super. OnCreate (saveinstanced); -Mnum=getarguments (). GetInt ("name"); -         intStyle=dialogfragment.style_no_title,theme=0; -         Switch((mNum-1)%6) +         { -          Case1: +style=Dialogfragment.style_no_title; A              Break; at          Case2: -style=Dialogfragment.style_no_frame; -           Case3: -style =Dialogfragment.style_no_input; -               Break; -           Case4:  instyle =Dialogfragment.style_normal; -               Break; to           Case5:  +style =Dialogfragment.style_normal; -               Break; the           Case6:  *style =Dialogfragment.style_no_title; $               Break;Panax Notoginseng           Case7: -style =Dialogfragment.style_no_frame; the               Break; +           Case8:  Astyle =Dialogfragment.style_normal; the               Break; +         } -         Switch((mNum-1)%6) $         { $          Case4: -Theme=android. R.style.theme_holo; Break; -          Case5: theTheme=Android. R.style.theme_holo_light_dialog; -              Break;Wuyi           Case6:theme = Android. R.style.theme_holo_light; Break; the           Case7:theme = Android. R.style.theme_holo_light_panel; Break; -           Case8:theme = Android. R.style.theme_holo_light; Break; Wu         } - SetStyle (style,theme); About     } $      PublicView Oncreateview (layoutinflater inflater,viewgroup contaniner,bundle savedinstance) -     { -View v=inflater.inflate (R.layout.textdialog, Contaniner,false); -TextView tv=(TextView) V.findviewbyid (r.id.text); ATv.settext ("dialog#" +mnum+ ": Using Style"); +         returnv; the      -     } $}
texdialogfragment

3. Call Textdialog

Invoking the dialog box you just defined in our activity, you first need to instantiate the dialog box, and then invoke the dialog's method according to the dialog box's needs. Finally, the show () method is called to display the dialog box.

1  voidShowDialog () {2mstacklevel++;3 4Android.app.FragmentTransaction ft =Getfragmentmanager (). BeginTransaction ();5Android.app.Fragment prev = Getfragmentmanager (). Findfragmentbytag ("dialog");6         if(Prev! =NULL) {7 Ft.remove (prev);8         }9Ft.addtobackstack (NULL);Ten  One         //Create and show the dialog. ADialogfragment newfragment =textdialogframnet.newinstance (mstacklevel); -Newfragment.show (FT, "dialog"); -}
ShowDialog

4.alertDialog

Alertdialog is a dialog box that the SDK encapsulates, which we can call directly. It is important to note that the class is static and can be called directly without instantiation.

1     NewAlertdialog.builder (mainactivity. This). SetIcon (R.drawable.ic_launcher)2. settitle ("delete Files"). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {3                     4 @Override5                      Public voidOnClick (Dialoginterface Dialog,intwhich) {6                         //TODO Auto-generated method stubs7                         NewAlertdialog.builder (mainactivity. This). Setmessage ("File has been deleted"). Create (). Show ();8                     }9                 })Ten. Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { One                      A @Override -                      Public voidOnClick (Dialoginterface Dialog,intwhich) { -                         //TODO Auto-generated method stubs the                         NewAlertdialog.builder (mainactivity. This). Setmessage ("You have selected the Cancel button, the file is not deleted"). Create (). Show (); -                     } -                 }) -. Show ();
Alertdialog

This code implements the prompt dialog box, when we delete, you can pop up the dialog box, and set the Setpositivebutton and Setnegativebutton two listening events, respectively, corresponding to the OK and Cancel button.

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.