"Turn" Pro Android Learning Note (44): Dialog (1): Trigger Dialog

Source: Internet
Author: User

Directory (?) [-]

    1. Create dialog Fragment
    2. Activity Display dialog box

Android provides alert, prompt, Pick-list, single, multi-Select, progress, Time-picker, and Date-picker dialogs, and provides custom dialog. After Android 3.0, dialog is based on fragment and provides a compatible support library for previous versions, meaning that for Developers, dialog is dialogfragment based, but at this point the relevant compatibility libraries need to be included in the app.

Unlike Windows or Web page JS's dialog, Android's dialog are asynchronous, not synchronous. For synchronized dialog, after the dialog is displayed, the next line of code waits until the dialog ends, that is, the next line of code can know the input of dialog and the button the user clicked. For asynchronous Dialog,dialog display, the next line of code continues to execute, instead of waiting for dialog to disappear, handling dialog events through callback. Asynchronous dialog also means that the applied code can also turn off dialog.

Our small example triggers an alarm box and a custom layout cue box with a menu trigger, with three buttons in the prompt box, and a Help button that triggers a dialog box for helping content.

Create dialog Fragment

dialog box based on Dialogframe, Alarm box alterdialogframent class, how to create an instance through Newinstance () has been learned in fragment learning, no longer detailed. Newinstance () has two parameters, one is the title of the Alarm box, the first is the contents of the Alarm box.

public class Alterdialogfragmentextends Dialogfragment{
/* "Step 1": Create an instance through Newinstance () and return, where the processing and system re-create the same from the save state.
* 1. Create an object from the default constructor
* 2. Set the passed information to the fragment parameter
* 3, return to the object
* */

public static alterdialogfragment newinstance (String title,string message) {
Alterdialogfragment ADF = new Alterdialogfragment ();
Bundle bundle = new bundle ();
Bundle.putstring ("Alert-title", title);
Bundle.putstring ("Alert-message", message);
Adf.setarguments (bundle);
return ADF;
}
...... A little, see the following article ...
}

The Custom layout hint box promptdialogfragment is also an inheritance of dialogfragment. Similarly, the code is as follows:

public class Promptdialogfragment extends Dialogfragment{

public static promptdialogfragment newinstance (String prompt) {
Promptdialogfragment PDF = new Promptdialogfragment ();
Bundle B = new bundle ();
B.putstring ("Prompt-message", prompt);
Pdf.setarguments (b);
return PDF;
}
...... A little, see the following article ...
}

Activity Display dialog box

In MyActivity, the display of the alarm box and the cue box is triggered by Optionsmenu, and the code is as follows:

public class Mainactivity extends activity{
Set the tag for the dialog fragment of the alarm box, the Prompt box, and the Help box.
Public final static String Alert_dialog_tag = "Alert_dialog_tag";
Public final static String Prompt_dialog_tag = "Prompt_dialog_tag";
Public final static String Help_dialog_tag = "Help_dialog_tag";

...... Slightly: Set the UI and create Optionsmenu ...

@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case R.id.alter_dialog:
Alterdialogtestcase ();
Break
Case R.id.prompt_dialog:
Promptdialogtestcase ();
Default
Break
}
return false;
}

/* Trigger Alarm box: triggered by Dialogfragment.show ()
* We note that for fragmenttransaction ft, the code does not perform Ft.commit (). View the source code for the Dialogfragment Show method, as follows
public voidShow (Fragmentmanager Manager, String tag){
mdismissed = false;
Mshownbyme = true;
Fragmenttransaction ft = manager.begintransaction ();
Ft.add (this, tag);
Ft.commit ();

}
public intShow (fragmenttransaction transaction, String tag){
mdismissed = false;
Mshownbyme = true;
Transaction.add (this, tag);
Mviewdestroyed = false;
Mbackstackid = transaction.commit ();
return mbackstackid;
}
* This operation contains Ft.add () and Ft.commit (), so it is not necessary to repeat the commit in the code, otherwise it will be abnormal. Add is added to the activity, where there is no ID for the container, i.e. Contianerviewid is 0, indicating that it is not loaded in a specific container, and null for Dialog,container.
* This example can also be achieved by Adf.show (Getfragmentmanager (), Alert_dialog_tag). For fragment transaction as a parameter, you can control it by fragment transaction before calling show (), such as adding to the back stack, which is demonstrated in the Help button pop-up box in the Prompt box. In Show (), the fragment tag is also set, which can be used for indexing, which can be obtained through Gettag () in fragment. */

private void Alterdialogtestcase () {
Alterdialogfragment ADF = alterdialogfragment.newinstance ("Alert", "This was the Alter Message for test!");
Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
adf.show (ft, alert_dialog_tag);
}
/* Popup Cue Box */
private void Promptdialogtestcase () {
Promptdialogfragment PDF = Promptdialogfragment.newinstance ("This is a Prompt dialog!");
Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Pdf.show (ft, prompt_dialog_tag);
}

/* This is the method that is called when the user presses a dialog box, and displays information about the toast. */
public void Ondialogdone (String tag, Boolean cancelled, charsequence message) {
String s = tag + "responds with:" + message;
if (cancelled)
s = tag + "was cancelled by the user";
//toast is a message box without a button that disappears after a certain amount of time and is well suited for debug.
Toast.maketext (this, S, Toast.length_long). Show ();
}

}

The benefit of implementing dialog through fragment is that the Fragment manager can automatically regain its original state without human intervention if the activity configuration changes (e.g., steering) are refactored.

The example code covered in this blog post can be downloaded in Pro Android Learning: Dialog small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (44): Dialog (1): Trigger Dialog

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.