Android 自定义对话框片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22214243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:39:12  来源:igfitidea点击:

Custom Dialog Fragment

androidandroid-fragmentsandroid-alertdialogandroid-dialogandroid-dialogfragment

提问by Coova

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

我正在尝试创建一个类似于DatePickerDialog. 的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 Dialogto show up? What should I be returning?

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

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

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

UPDATED: FIXED PROBLEM, CODE SHOWN BELOW IS FUNCTIONING

更新:固定问题,下面显示的代码正在运行

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

THE FRAGMENT WILL BE USED LIKE THIS

片段将像这样使用

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

回答by Philippe A

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

如果要在其中打开DialogFragmentextends 的活动FragmentActivity,则应执行:

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

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

您还需要在onCreateDialog对话框片段类的方法中扩充对话框布局文件。

Using AlertDialog.Builderyou can achieve all this easily, e.g.

使用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();

There are many examples on http://developer.android.com/reference/android/app/DialogFragment.html

http://developer.android.com/reference/android/app/DialogFragment.html上有很多例子

回答by Pankaj

There are different ways you can create your dialog either using OnCreateViewmethod or using OnCreateDialogmethod if you are extending the DialogFragmentclass and so the implementation goes in a different way for both.

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

You should return your Dialoginstance from OnCreateDialogmethod.

您应该DialogOnCreateDialog方法返回您的实例。

How to show the dialog in a Activity

如何在对话框中显示 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();

May be you can refer to this linkwhich is a nice example from official AndroidDeveloper's site.

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