定制对话框片段

7
我正在尝试创建一个类似于DatePickerDialog的简单对话框。我正在创建的Dialog应该向用户提供一个图像数组,供他们选择。
我相信我已经成功创建了数组并添加了正确的图像,现在遇到的问题是如何让Dialog出现?我应该返回什么?
我已经研究过AlertDialogs等,但我不确定如何实现它们。 更新:问题已解决,下面的代码正在运行 图片选择器
public class PicturePickerFragment extends DialogFragment {

ArrayList<Integer> imageList = new ArrayList<Integer>();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // fill an array with selected images

    String title = "Picture";

    imageList.add(R.drawable.barbershop);
    imageList.add(R.drawable.wedding);
    imageList.add(R.drawable.meeting);
    imageList.add(R.drawable.barbershop);

    // return alertdialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.activity_create, null))
            .setTitle(R.string.event_type)
            .setPositiveButton(R.string.select_picture,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // call the method on the parent activity when
                            // user click the positive button
                        }
                    });
    return builder.create();
}

}

供参考

public class DatePickerFragment extends DialogFragment implements
    DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    month++;
    String dateString = month + "/" + day + "/" + year;
    Button b = (Button) getActivity().findViewById(R.id.btn_date);
    b.setText(dateString);
}
}

片段将被如此使用

<Button
    android:id="@+id/btn_picture"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="15dp"
    android:layout_weight="1"
    android:background="@null"
    android:drawablePadding="15dp"
    android:onClick="showPicturePickerDialog"
    android:drawableTop="@drawable/ico_picture"
    android:text="Picture"
    android:textColor="@color/darkbrown"
    android:textSize="20sp" />

请在发布问题之前参考此链接 http://developer.android.com/reference/android/app/DialogFragment.html,并进行谷歌搜索。 - Ando Masahashi
2个回答

8

如果您想打开DialogFragment的活动扩展了FragmentActivity,则应执行以下操作:

PicturePickerFragment dialog = new PicturePickerFragment();
dialog.show(getSupportFragmentManager(), "YourDialog");

你需要在对话框片段类的onCreateDialog方法中填充对话框布局文件。使用AlertDialog.Builder可以轻松实现此操作,例如:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialoglayout, null))
 .setTitle(R.string.dialog_title)
 .setPositiveButton(R.string.pos_button, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // call the method on the parent activity when user click the positive button
    }
})
 .setNegativeButton(R.string.neg_button, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // call the method on the parent activity when user click the negative button
    }
});
return builder.create();

http://developer.android.com/reference/android/app/DialogFragment.html上有许多关于IT技术的示例。


完美。我现在想添加一个图片数组,用户可以从中选择。当我使用.setView()时,我应该使用一个包含ListView的布局来持有图像数组吗? - Coova
是的,您的对话框布局应该包含一个ListView。您可以使用扩展ArrayAdapter的自定义适配器填充列表,该适配器包含您的图像数组。如果需要帮助,请提出另一个问题,因为这是完全不同的主题 :) - Philippe A

0

你可以使用OnCreateView方法或者OnCreateDialog方法来创建对话框,如果你正在扩展DialogFragment类,那么两种方法的实现方式会有所不同。

你应该从OnCreateDialog方法中返回你的Dialog实例。

如何在Activity中显示对话框

 FragmentTransaction ft = getFragmentManager().beginTransaction();
 Fragment prev = getFragmentManager().findFragmentByTag("YourFragmentID");

 if (prev != null) {
        ft.remove(prev);
    }
 ft.addToBackStack(null);

 PicturePickerFragment pf = new PicturePickerFragment();
 pf.show();

也许你可以参考一下链接,这是来自官方Android开发者网站的一个很好的例子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接