利用Fragment来制作对话框

package com.example.myfirstapp;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class ListItemDialogFragment extends DialogFragment {
	 CharSequence[] items={"Google","Apple","Microsoft"};
     boolean[] itemsChecked=new boolean[items.length];

 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
     
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     
        builder.setTitle("这是一个多项选择对话框")
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                       Toast.makeText(getActivity(),
                           "You click it!", Toast.LENGTH_SHORT).show();
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                       Toast.makeText(getActivity(),
                           "Cancel clicked!", Toast.LENGTH_SHORT).show();
                   }
               })
               .setMultiChoiceItems(items, itemsChecked, new
            DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id,
                boolean isChecked) {
                    Toast.makeText(getActivity(),
                        items[id] + (isChecked ?  " checked!":
                        " unchecked!"),
                        Toast.LENGTH_SHORT).show();
                }
            }
        );
        // Create the AlertDialog object and return it
        return builder.create();
    }

}
上面写的是一个对话框Fragment,下面是写在Activity里要调用该对话框的语句




        Button btn_dialog=(Button) findViewById(R.id.btn_dialog);
        
        //调用另外的fragment对话框
        btn_dialog.setOnClickListener(new View.OnClickListener()
        {
        	

			@Override
			public void onClick(View arg0) {

		        ListItemDialogFragment dialogFragment = new ListItemDialogFragment();
		        	
		             dialogFragment.show(getFragmentManager(), "dialog1");  
				
				
		
			}
        });

你可能感兴趣的:(利用Fragment来制作对话框)