Create DialogFragment dialog in Fragment

                                                            Dialog box under AUF law

 

[Wedge] It seems very simple to do DiaLog on the Activity page. Most of them will use it after learning the controls, but google hopes that developers will only use FragementDialog. It is self-customization. In fact, it is to design an xml layout and then display it as a dialog box. This free-form dialog box is naturally much better than the Activity itself.

 

[DialogFragment basics] If you want to make a fragmented dialog, you need to understand the DialogFragment class. This class inherits from Fragment, so the behavior is similar.

[Realization effect]: Click the island timing in a fragment to jump out of the dialog box



 

 

[The first step to create a DialogFragment]

First create the dialog.xml file like Fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="110dp"
            android:textSize="30dp"
            android:text="时" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
             android:textSize="30dp"
            android:text="分" />

    </LinearLayout>

    <TimePicker
        android:id="@+id/mytime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/timesure"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#33A18F"
        android:textColor="#FFFFFF"
        android:layout_marginBottom="20dp"
        android:text="OK" />

</LinearLayout>

 

Next is the DialogFragment class:

package com.example.testdrawerlayoutleft;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class TimeDialog extends DialogFragment{
	  private TimePicker mytime=null;
	  private Button timesubmit;
	  public int hour;
	  public int minute;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		  getDialog().setTitle("Countdown");
		  View view=inflater.inflate(R.layout.dialog, container);
		  mytime=(TimePicker)view.findViewById(R.id.mytime);  
		  timesubmit = (Button) view.findViewById (R.id.timesure);
		  timesubmit.setOnClickListener (new OnClickListener () {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 OnFragmentListener listener=(OnFragmentListener)getActivity();
			       listener.onFragmentListener(hour, minute);
				getDialog().dismiss();
				
			}
		});
		  mytime.setIs24HourView(true);
		  mytime.setCurrentHour(13);  
	      mytime.setCurrentMinute(14);  
	      mytime.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute1) {
				// TODO Auto-generated method stub
			     hour=hourOfDay;
			     minute=minute1;
			     Toast.makeText(getActivity(), hour+" "+minute, 100).show();
			}
		});
	      
		return view;
	}

}

 
Key code:

public class TimeDialog extends DialogFragment

 

public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)

 
[Fragment processing in click event]:

Listen to the radiobutton event:

countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			main.showDialog("dialog");
			
		}
	});

 The main is the main Activity, and the way to get it is as follows:

Main main = (Main) getActivity();

 The management dialog display method is still written in the main Activity:

public void showDialog(String tag){
		TimeDialog dialog=new TimeDialog();
		  dialog.show(getFragmentManager(), tag);
	}

 It's all done here, isn't it simpler?

There are two ways to display the fragment dialog

1.public int show(FragmentTransaction transaction ,String tag)

2.public int show(Fragment manager, String tag)

The difference is that the first one enters the transaction and enters the return stack at the same time

The second just gets a transaction for display.

 

The advantage of the fragment dialog is that even if the device is rotated, the fragment manager will perform basic state management, and we don't need to do any state management, the dialog will be rebuilt.
 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326715477&siteId=291194637