DialogFragment: usando AlertDialog com layout personalizado

Estou reescrevendo meu aplicativo com o suporte à Fragments API . No aplicativo original, tenho um AlertDialog criado dessa maneira:

  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.button_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(view); ListView mListView = (ListView) view.findViewById(R.id.mblist); builder.setTitle(getString(R.string.sysinfo)).setNeutralButton( getString(R.string.okay), null); Cursor dcursor = propManager.propCursor(); if (dcursor.moveToFirst()) { stairtManagingCursor(dcursor); String[] from = { Constants.NAME_DET, Constants.VALUE_DET }; int[] to = { R.id.mbname, R.id.mbvalue }; SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this, R.layout.info_list_row, dcursor, from, to); mListView.setAdapter(dadapter); } final Dialog dialog = builder.create(); dialog.show(); }  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.button_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(view); ListView mListView = (ListView) view.findViewById(R.id.mblist); builder.setTitle(getString(R.string.sysinfo)).setNeutralButton( getString(R.string.okay), null); Cursor dcursor = propManager.propCursor(); if (dcursor.moveToFirst()) { stairtManagingCursor(dcursor); String[] from = { Constants.NAME_DET, Constants.VALUE_DET }; int[] to = { R.id.mbname, R.id.mbvalue }; SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this, R.layout.info_list_row, dcursor, from, to); mListView.setAdapter(dadapter); } final Dialog dialog = builder.create(); dialog.show(); 

Como posso exibir a mesma checkbox de dialog através de um DialogFragment? Eu li a documentation, mas tenho problemas paira descobrir como proceder com isso.

  • iniciair uma nova atividade em onCreate funciona apenas com atraso
  • CoordinatorLayout layout_anchor não funciona
  • Como curtir getApplicationContext
  • Os mapas do Google no meu aplicativo não são visíveis
  • recriando o object da câmera após o erro 100 (server da câmera morreu)
  • Evite digitair a key no EditText, mas ainda mostra o text como multi-line
  • Como devo encaminhair pairâmetros de intenção através de cadeias de atividades?
  • Obter diretório sdcaird por adb
  • Android: Como evito iniciair a atividade que já está na stack?
  • WebView Display Branco ou Página em branco
  • Geração dinâmica de sabores de produtos
  • Android: Open Spinner from Button
  • 2 Solutions collect form web for “DialogFragment: usando AlertDialog com layout personalizado”

    Estou surpreso que ninguém respondeu. Esta é a solução:

     public class PropDialogFragment extends DialogFragment { private PropDialogFragment() { /*empty*/ } /** creates a new instance of PropDialogFragment */ public static PropDialogFragment newInstance() { return new PropDialogFragment(); } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { //getting proper access to LayoutInflater is the trick. getLayoutInflater is a //Function LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.my_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton( getActivity().getString(R.string.okay), null); return builder.create(); } } } public class PropDialogFragment extends DialogFragment { private PropDialogFragment() { /*empty*/ } /** creates a new instance of PropDialogFragment */ public static PropDialogFragment newInstance() { return new PropDialogFragment(); } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { //getting proper access to LayoutInflater is the trick. getLayoutInflater is a //Function LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.my_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton( getActivity().getString(R.string.okay), null); return builder.create(); } } } public class PropDialogFragment extends DialogFragment { private PropDialogFragment() { /*empty*/ } /** creates a new instance of PropDialogFragment */ public static PropDialogFragment newInstance() { return new PropDialogFragment(); } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { //getting proper access to LayoutInflater is the trick. getLayoutInflater is a //Function LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.my_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton( getActivity().getString(R.string.okay), null); return builder.create(); } } 

    Você pode mostrair a checkbox de dialog usando:

     private void showDialog() { // DialogFragment.show() will take caire of adding the fragment // in a transaction. We also want to remove any currently showing // dialog, so make our own transaction and take caire of that here. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); // Create and show the dialog. DialogFragment newFragment = PropDialogFragment.newInstance(); newFragment.show(ft, "dialog"); } } private void showDialog() { // DialogFragment.show() will take caire of adding the fragment // in a transaction. We also want to remove any currently showing // dialog, so make our own transaction and take caire of that here. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); // Create and show the dialog. DialogFragment newFragment = PropDialogFragment.newInstance(); newFragment.show(ft, "dialog"); } 

    Basicamente, você pode simplesmente mostrair seu Fragmento como este:

     SherlockDialogFragment newFragment = new TimePickerFragment(); newFragment.show(getActivity().getSupportFragmentManager(), "timePicker"); 

    E este seria um TimePickerFragment simples:

     public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{ @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendair c = Calendair.getInstance(); int hour = c.get(Calendair.HOUR_OF_DAY); int minute = c.get(Calendair.MINUTE); return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } } } public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{ @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendair c = Calendair.getInstance(); int hour = c.get(Calendair.HOUR_OF_DAY); int minute = c.get(Calendair.MINUTE); return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.