Introduction to Android Development (13) special fragment 13.2 Dialogfragment

Source: Internet
Author: User

We can also create another fragment of--dialogfragment. As the name suggests, Dialogfragment is a fragment floating above the activity. Dialogfragment comes in handy when the user's feedback is needed. Similar to using listfragment, you need to inherit the Dialogfragment base class.

The following will show you how to use Dialogfragment.

1. Create a project: Dialogfragmentexample.

2. Create a new class below the package path, Fragment1.

public class Fragment1 extends Dialogfragment {static Fragment1 newinstance (String title) {     
        Fragment1 fragment = new Fragment1 ();     
        Bundle args = new Bundle ();     
        Args.putstring ("title", title);     
        Fragment.setarguments (args);     
    return fragment; @Override public Dialog Oncreatedialog (Bundle savedinstancestate) {String title =     
        Getarguments (). getString ("title");     
        return new Alertdialog.builder (Getactivity ()). SetIcon (R.drawable.ic_launcher). Settitle (title)  . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {(Dialogfragmentex     
            ampleactivity) getactivity ()). Dopositiveclick (); }). SetnegAtivebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Di     
                        Aloginterface dialog, int whichbutton) {(dialogfragmentexampleactivity)     
            Getactivity ()). Donegativeclick ();     
    ). Create (); }             
         
}

3. Code in the Dialogfragmentexampleactivity.java.

The public class Dialogfragmentexampleactivity extends activity {/** called the ' when the ' is ' the activity ' is a     
    -created 
    Verride public 
    void OnCreate (Bundle savedinstancestate) {     
        super.oncreate (savedinstancestate);     
        Setcontentview (r.layout.main);     
                 
        Fragment1 dialogfragment = fragment1.newinstance (     
                "Are you sure to do this?");     
            Dialogfragment.show (Getfragmentmanager (), "dialog");        
    }     
             
    public void Dopositiveclick () {     
        //---Perform steps when user clicks on OK---     
        log.d ("Dialogfragmentexample", " User clicks on OK);     
    }     
         
    public void Donegativeclick () {     
        //---Perform steps when user clicks on Cancel---     
        log.d ("Dialogfragmentexample "," User clicks on Cancel ");     
    }     
         

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.