自定义对话框片段 [英] Custom Dialog Fragment

本文介绍了自定义对话框片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a simplistic dialog similar to a DatePickerDialog. The Dialog that I am creating should provide the user with an array of images from which they can select.

我相信我已经成功创建了数组并向其中添加了正确的图像,我现在遇到的问题是如何显示Dialog?我应该返回什么?

I believe I have managed to create the array and add the correct images to it, the problem I am running into now is how to get the Dialog to show up? What should I be returning?

我已经研究过AlertDialogs之类的东西,但是我不确定该怎么做.

I have looked into AlertDialogs and such, but I am not sure as to what to do to implement them.

已更新:已修复问题,下面显示的代码正在起作用

图片选择器

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

推荐答案

如果要打开DialogFragment的活动扩展了FragmentActivity,则应执行:

If the activity in which you want to open the DialogFragment extends FragmentActivity, you should execute:

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

此外,您还需要使用对话框片段类的方法onCreateDialog来填充对话框布局文件.

Also you need to inflate your dialog layout file in the method onCreateDialog of your dialog fragment class.

使用AlertDialog.Builder您可以轻松实现所有这些目标,例如

Using AlertDialog.Builder you can achieve all this easily, e.g.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆