Android Program Development: (13) Special Fragment-13.2 DialogFragment

Source: Internet
Author: User

We can also create another Shard, DialogFragment. As the name implies, DialogFragment is a Fragment floating on the Activity. DialogFragment is useful when user feedback is required. Similar to ListFragment, The DialogFragment base class must be inherited.

The following describes how to use DialogFragment.

1. Create a project: DialogFragmentExample.

2. Create a class, Fragment1, under the package path.

[Java]
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 ){
(DialogFragmentExampleActivity)
GetActivity (). doPositiveClick ();
}
})
. SetNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
(DialogFragmentExampleActivity)
GetActivity (). doNegativeClick ();
}
}). Create ();
}
 
}
3. Code in DialogFragmentExampleActivity. java.
[Java]
Public class DialogFragmentExampleActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Fragment1 dialogFragment = Fragment1.newInstance (
"Are you sure you want 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 ");
}
 
}
4. Press F11 to debug the simulator. A dialog box is displayed. Click OK or Cancel to bring up the message.


To create a DialogFragment, you must first inherit the DialogFragment base class:

[Java]
Public class Fragment1 extends DialogFragment {
}
In this example, I did not create a warning dialog box, which contains a warning message and two buttons available for clicking. In the Fragment1 class, we define a newInstance () method:
[Java]
Static Fragment1 newInstance (String title ){
Fragment1 fragment = new Fragment1 ();
Bundle args = new Bundle ();
Args. putString ("title", title );
Fragment. setArguments (args );
Return fragment;
}
This newInstance () method allows you to create a fragmented instance object. It can also accept a specified string parameter, which is the message in the dialog box. The title object is stored in a Bundle object and will be used later.
Next, we define the onCreateDialog () method, which is called after onCreate () and before onCreateView:

[Java]
@ 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 ){
(DialogFragmentExampleActivity)
GetActivity (). doPositiveClick ();
}
})
. SetNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
(DialogFragmentExampleActivity)
GetActivity (). doNegativeClick ();
}
}). Create ();
}
Then, we can create two buttons, OK and Cancel. The title of the dialog box is obtained from the Bunddle object.
To display the dialog box fragments, create an instance and call the show () method:

[Java]
Fragment1 dialogFragment = Fragment1.newInstance (
"Are you sure you want to do this? ");
DialogFragment. show (getFragmentManager (), "dialog ");
At the same time, we need to implement two callback Methods: doPositiveClick () and doNegativeClick (). These two methods are used to process user input events:
[Java]
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 ");
}
Note: After Android 4.0, it is recommended that DialogFragment be used to replace Dialog. Let's take a look at the Android 4.0 Launcher and other source code. The Fragment class is widely used in these source code.

 

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.