[-]
- Rewrite Oncreateview
- Setting up UI and key feedback via Oncreateview
- Information preservation
- Rewrite Oncreatedialog
Dialogfragment Instance newinstance () has been implemented in the last study note. We create the dialog UI, which can be implemented by overriding one of the two functions of dialogfragment, which is Oncreateview () and Oncreatedialog (), which returns the view, which returns dialog, Like through Alertdialog.builder constructs.
Rewrite Oncreateview ()
overriding Oncreateview () is the traditional way of fragment, suitable for custom dialogs, and this example is suitable for use in a prompt box, as shown in. By pressing the menu pop-up cue box, the cue box consists of a textview, a edittext, and three buttons that make up the UI. Press different buttons to trigger different processing. As a small example, by pressing the Save and dismiss buttons, the activity's Ondialogdone () function is invoked to display different information depending on the user's actual operation. Press the Help button to bring up a helper box. The Reload box is implemented later in the study notes.
Setting up UI and key feedback via Oncreateview ()
Using Fragment's Oncreateview () to implement dialog UI and fragment learning, there is no difference in this example, we added a button click on the trigger, the code is as follows:
public class Promptdialogfragmentextends Dialogfragment implements Onclicklistener{
public static promptdialogfragment newinstance (String prompt) {
... Slightly...
}
@Override//Implement dialog UI by rewriting fragment's Oncreateview ()
Public ViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
1, through inflate, according to layout XML definition, create view
View v = inflater.inflate (R.layout.prompt_dialog, Container,false);
TextView TV = (TextView) V.findviewbyid (r.id.prompt_message);
Tv.settext (Getprompt ());
2, register three button of the key monitoring listener
Button dismissbtn = (button) V.findviewbyid (R.id.button_dismiss);
Dismissbtn.setonclicklistener (this);
Button savebtn = (button) V.findviewbyid (R.id.button_save);
Savebtn.setonclicklistener (this);
Button helpbtn = (button) V.findviewbyid (R.ID.BUTTON_HELP);
Helpbtn.setonclicklistener (this);
return v;
}
Private String getprompt () {
Bundle B = getarguments ();
Return b.getstring ("Prompt-message");
}
@Override//Set the style, properties, etc. of the dialog box in OnCreate
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
//If the parameter in Setcancelable () is True, if you click on an activity that is not covered by the dialog or press the back key, then cancel, state detection OnCancel () and Ondismiss (). If the argument is false, the blank or return key does not respond. Default is True
setcancelable (true);
You can set the display style of the dialog, such as the style style_no_title, and the TITLE will be displayed. Unfortunately, I did not find a way to set the title content in Dialogfragment. The theme is 0, indicating that the appropriate theme is selected by the system.
int style = dialogfragment.style_no_normal, theme = 0;
SetStyle (Style,theme);
}
@Override//For status tracking only
public void OnCancel (Dialoginterface dialog) {
Showinfo ("OnCancel () is called");
Super.oncancel (Dialog);
}
@Override//user state tracking only
public void Ondismiss (Dialoginterface dialog) {
Showinfo ("Ondismiss () is called");
Super.ondismiss (Dialog);
}
@Override//button button-triggered callback function
public void OnClick (View v) {
Mainactivity act = (mainactivity) getactivity ();
Switch (V.getid ()) {
Case R.id.button_dismiss:
Act.ondialogdone (Gettag (), true, NULL); Invoke Activity's Ondialogdone () to display related information through toast
dismiss ();//Closes the dialog box and triggers the Ondismiss () callback function.
Break
Case R.ID.BUTTON_HELP:
... Slightly: Later realized ...
Break
Case R.id.button_save:
TextView TV = (TextView) GetView (). Findviewbyid (R.id.input_text);
Act.ondialogdone (Gettag (), False, "[Save]" + tv.gettext ()); Invoke Activity's Ondialogdone () to display related information through toast
dismiss ();//Close the dialog box and trigger the Ondismiss () callback function
Break
Default
Break
}
}
private void Showinfo (String s) {
LOG.D ("Promptdialogfragment", s);
}
}
Information preservation
If the user fills in the text in the input box, then carries on the screen the horizontal screen and the vertical screen switch, this involves fills in the content the preservation, May through Onsaveinstancestate (), saves it to the fragment bundle Savedinstancestate, and restore it in Oncreateview (). However, in the Android 4.2 version of the test, the system has been able to automatically save and restore, no need to add code. Of course, we still recommend the following treatment in a safe way.
public class Promptdialogfragment extends Dialogfragment implements onclicklistener{
Private EditText et = null;
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
......
ET = (EditText) V.findviewbyid (R.id.input_text);
if (savedinstancestate! = null) {
Charsequence Text = savedinstancestate.getcharsequence ("input");
Et.settext (Text = = null?) "": text);
}
......
}
@Override
public void Onsaveinstancestate (Bundle outstate) {
Outstate.putcharsequence ("Input", Et.gettext ());
Super.onsaveinstancestate (outstate);
}
}
Rewrite Oncreatedialog ()
For a simple dialog box, the UI of the dialog can be created directly via Alterdialog.builder, this example is used for alarm boxes, such as. Alertdialog.builder before the Android 3.0 version of the Create dialog, in a later version, available in dialogfragment, for creating a simple dialog box.
The code is as follows. Although all are Onclicklistener interface, but the prompt box is View.onclicklistener, here is Dialoginterface.onclicklistener.
public class Alterdialogfragmentextends Dialogfragment implements Dialoginterface.onclicklistener{
/* "Step 1": Create an instance from Newinstance () and return * */
public static alterdialogfragment newinstance (String title,string message) {
... Slightly...
}
Private String GetTitle () {
Return Getarguments (). getString ("Alert-title");
}
Private String GetMessage () {
Return Getarguments (). getString ("Alert-message");
}
/ * "Step 2" CREATE view can be done in two ways, one is Oncreateview () in fragment, and the other is Oncreatedialog () in Dialogfragment.
* The former is suitable for setting the custom layout, with greater flexibility
* While the latter is suitable for handling simple dialog, you can use Dialog.builder to return dialog objects directly
* From the life cycle sequence, Oncreatedialog () is executed first, then Ooncreateview () is executed, and we should not use both.
* */
@Override
Public Dialog Oncreatedialog (Bundle savedinstancestate) {
Alertdialog.builder B = New Alertdialog.builder (getactivity ())
. Settitle (GetTitle ())
. Setmessage (GetMessage ())
. Setpositivebutton ("OK", this)Setting the callback function
. Setnegativebutton ("Cancel", this);Setting the callback function
return b.create ();
}
@Override//Key-triggered callback function
Public void OnClick (dialoginterface dialog, int which) {
Boolean iscancel = false;
if (which = = alertdialog.button_negative){//Judge the user by pressing Hejian
Iscancel = true;
}
MyActivity act = (myactivity) getactivity ();
Act.ondialogdone (Gettag (), Iscancel, "Click OK, Alert dismissed");
}
}
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 (four or five): Dialog (2): Dialogfragment