package
leihu.dialogtoactivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
@SuppressLint ( "NewApi" )
public
class NoticeDialogFragment extends
DialogFragment{
public
interface NoticeDialogListener{
public
void onDialogPositiveClick(DialogFragment dialog);
@SuppressLint ( "NewApi" )
public
void onDialogNegativeClick(DialogFragment dialog);
}
NoticeDialogListener mListener;
@SuppressLint ( "NewApi" )
@Override
public
void onAttach(Activity activity) { //Called when a fragment is first attached to its activity //<code><a href="http://i.cnblogs.com/reference/android/app/Fragment.html#onCreate(android.os.Bundle)">onCreate(Bundle)</a></code> will be called after this.
super .onAttach(activity);
try
{
mListener = (NoticeDialogListener) activity;
} catch
(Exception e) {
throw
new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener" );
}
}
@SuppressLint ( "NewApi" )
@Override
public
Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
builder.setMessage( "fire missiles" )
.setPositiveButton( "frie" , new
DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog, int
which) {
mListener.onDialogPositiveClick(NoticeDialogFragment. this );
}
})
.setNegativeButton( "cancel" , new
DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog, int
which) {
mListener.onDialogNegativeClick(NoticeDialogFragment. this );
}
});
return
builder.create();
}
}
|