public class MyDialogFragment extends DialogFragment { private EditText mEditText; private String mText;
public MyDialogFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_dialog, null);
mEditText = (EditText) view.findViewById(R.id.editText);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setTitle("Dialog Fragment Title")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mText = mEditText.getText().toString();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
AlertDialog dialog = builder.create();
setCancelable(false);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
}
MyDialogFragment dialogFragment = new MyDialogFragment(); dialogFragment.show(getSupportFragmentManager(), "dialog");