gpt4 book ai didi

android - 自定义对话框 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:04 26 4
gpt4 key购买 nike

我正在尝试创建一个类似于 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);
}
}

fragment 将像这样使用

<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 的 Activity 扩展了FragmentActivity,您应该执行:

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

您还需要在对话框 fragment 类的方法 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上有很多例子

关于android - 自定义对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com