【问题标题】:DialogFragment : Using AlertDialog with custom layoutDialogFragment : 使用带有自定义布局的 AlertDialog
【发布时间】:2012-05-14 08:07:02
【问题描述】:

我正在使用Fragments API 支持重写我的应用程序。在原始应用程序中,我有一个 AlertDialog,它是这样创建的:

    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()) {
        startManagingCursor(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();

如何通过 DialogFragment 显示相同的对话框?我已阅读文档,但不知道如何继续。

【问题讨论】:

    标签: android android-fragments android-alertdialog


    【解决方案1】:

    我很惊讶没有人回答。这是解决方案:

    public class PropDialogFragment extends DialogFragment {
    
        private PropDialogFragment() { /*empty*/ } 
    
        /** creates a new instance of PropDialogFragment */
        public static PropDialogFragment newInstance() {
            return new PropDialogFragment();
        }
    
        @Override
        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();
        }
    }
    

    您可以使用以下方法显示对话框:

    private void showDialog() {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care 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");
    }
    

    【讨论】:

    • 这很有帮助。谢谢。
    • 你宁愿getActivity().getLayoutInflater()而不是调用系统服务和强制转换吗?
    • 很棒的答案。以防万一有人好奇,the docs cover this
    • 我认为fragment transactions 是显示AlertDialog 所必需的。但现在我直接从DialogFragment 使用dialog.show(); 并且它有效。
    • 我知道这是旧的,但是片段不能有私有的默认构造函数,因为它们可以被系统重新创建。
    【解决方案2】:

    基本上你可以像这样展示你的片段:

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

    这将是一个简单的 TimePickerFragment:

    public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
    
    
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2022-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多