自定义对话框片段

库瓦

我正在尝试创建类似于的简单对话框DatePickerDialogDialog,我应该创造提供,他们可以选择像阵列的用户。

我相信我已经成功创建了数组并向其中添加了正确的图像,我现在遇到的问题是如何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" />
菲利普·A

如果要在其中打开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上有很多示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android自定义对话框片段崩溃

来自分类Dev

来自自定义对话框的android片段调用

来自分类Dev

WXPYTHON自定义对话框

来自分类Dev

自定义对话框太小

来自分类Dev

自定义进度对话框

来自分类Dev

自定义对话框错误

来自分类Dev

自定义引导对话框

来自分类Dev

自定义对话框的布局

来自分类Dev

调用设置方法时自定义对话框片段崩溃

来自分类Dev

如何在从片段打开的自定义对话框中保留侦听器?

来自分类Dev

显示TextView时对话框片段(自定义布局)不起作用

来自分类Dev

如何获取在片段中输入到自定义对话框中的信息?

来自分类Dev

自定义对话框片段中带有自定义对象列表的微调器不起作用

来自分类Dev

使用Python创建SPSS自定义对话框

来自分类Dev

Android自定义对话框样式

来自分类Dev

自定义对话框打开时强制关闭

来自分类Dev

jQuery UI自定义对话框按钮

来自分类Dev

如何从自定义对话框中删除标题?

来自分类Dev

如何自定义放置Jquery对话框

来自分类Dev

Electron中的自定义HTML对话框

来自分类Dev

在Chain Wix中插入自定义对话框

来自分类Dev

DatePicker的内容与自定义对话框的标题重叠

来自分类Dev

如何制作更好的自定义对话框?

来自分类Dev

设置自定义对话框的高度和宽度

来自分类Dev

在自定义对话框中设置textview

来自分类Dev

设置自定义对话框的时间

来自分类Dev

没有焦点的自定义对话框

来自分类Dev

在自定义对话框中填充NumberPicker

来自分类Dev

自定义视图中的Android显示对话框