0

好的 Activity 类型的方法 showDialog(int, Bundle) 已弃用...所以我已经将 timePicker 更改为 fragmentDialog ,这很容易,因为 fragmentDialog 已经准备好:

new TimePickerDialog(getActivity(),_listener, _hour, _minute,dateFormat);

但是我怎样才能将这种类型的对话框重新制作为 fragmentDialog 呢?

AlertDialog.Builder
        .setIcon()
        .setTitle()
        .setPositiveButton()
        .setSingleChoiceItems(new CharSequence[]{"Visual","Audio","Both"},2,null)

谢谢你。

4

1 回答 1

0

如果我对您的理解正确,您想重用您的AlerDialog,以便您可以定义一次并在片段/活动中的不同位置使用。

来自android文档AlertDialog android.app.AlertDialog.Builder.create() Creates a AlertDialog with the arguments supplied to this builder. It does not show() the dialog. This allows the user to do any extra processing before displaying the dialog. Use show() if you don't have any other processing to do and want this to be created and displayed. 所以:

//declare inside your fragment class
private AlertDialog welcomeDialog;

//Inisde onCreate paste the following code so your dialoge is just created but not shown
            welcomeDialog = new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(
                        getResources().getString(R.string.first_run_title))
                .setMessage(
                        getResources().getString(R.string.first_run_desc))
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        }).create();

//Wherever in your code you need to display the dialog,just use this piece of code:
welcomeDialog.show();

//and finally to do the cleanup:
    @Override
protected void onStop() {
    super.onStop();
    if (welcomeDialog != null)
        welcomeDialog.dismiss();}
于 2013-03-14T19:48:44.610 回答