自定义对话框片段

时间:2014-03-06 03:28:02

标签: android android-fragments alertdialog android-dialog android-dialogfragment

我正在尝试创建一个类似于DatePickerDialog的简单对话框。我正在创建的Dialog应该为用户提供一系列图像,供他们选择。

我相信我已经成功创建了数组并为其添加了正确的图像,我现在遇到的问题是如何让Dialog显示出来?我该怎么回事?

我已经研究过AlertDialogs等等,但我不知道如何实施它们。

更新:固定问题,代码下面显示的是功能

PICTURE PICKER

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();
}

}

FOR REFERENCE

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" />

2 个答案:

答案 0 :(得分: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

上有很多例子

答案 1 :(得分:1)

如果要扩展OnCreateView类,可以使用OnCreateDialog方法或使用DialogFragment方法创建对话框的方法不同,因此实现方式不同

您应该从Dialog方法返回OnCreateDialog个实例。

如何在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();

您可以参考这个link,这是官方AndroidDeveloper网站上的一个很好的例子。