= -6ren">
gpt4 book ai didi

java - 如何在 fragment 中实现 Android 日期选择器

转载 作者:行者123 更新时间:2023-11-29 21:29:57 24 4
gpt4 key购买 nike

我已经实现了一个日期选择器,如下所示。

private void updateDisplay() {
String str_date = "";
if ((Month + 1) >= 10) {
if (Day < 10) {
str_date = Year + "-" + (Month + 1) + "-0" + Day;
} else {
str_date = Year + "-" + (Month + 1) + "-" + Day;
}
} else {
if (Day < 10) {
str_date = Year + "-0" + (Month + 1) + "-0" + Day;
} else {
str_date = Year + "-0" + (Month + 1) + "-" + Day;
}

}
date.setText("" + str_date);
}

protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(Year, Month, Day);
break;
}
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(viewLoad.getContext(),
mDateSetListener, Year, Month, Day);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
updateDisplay();
}
};

这是我的按钮代码段。

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDate:
showDialog(DATE_DIALOG_ID);
break;

这很好用。现在的问题是我需要在一个 fragment 中实现一个日期选择器。谁能解释一下如何去做。

编辑代码

 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
updateDisplay();
}
};

提前致谢。

最佳答案

使用这个包含使用

CustomDialogFragment
  1. TimePicker 对话框 fragment
  2. DatePicker 对话框 fragment
  3. 简单的对话框 fragment

    CustomeDialogFragment 类

公共(public)类 CustomDialogFragment 扩展 DialogFragment{

            public static final int DATE_PICKER = 1;
public static final int TIME_PICKER = 2;
public static final int DIALOG = 3;

Context mContext;
String mMessage;
boolean finishActivity;

// private Fragment mCurrentFragment;
Activity mActivity;

/**
* this constructor is used for datepicker & Timepicker
*/
public CustomDialogFragment (Fragment fragment ) {
// mCurrentFragment = fragment;
}

public CustomDialogFragment (Activity mActivity ) {
this.mActivity = mActivity;
}

/**
* this constructor is used for simple dialog fragment
*/
public CustomDialogFragment(Context context, String message, final boolean finishActivity) {
mContext = context;
mMessage = message;
this.finishActivity = finishActivity;
}



public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = new Bundle();
bundle = getArguments();
int id = bundle.getInt("dialog_id");
switch (id) {
case DATE_PICKER:
return new DatePickerDialog(getActivity(),
(OnDateSetListener)mActivity, bundle.getInt("year"),
bundle.getInt("month"), bundle.getInt("day"));

case TIME_PICKER:
return new TimePickerDialog(getActivity(),
(OnTimeSetListener)mActivity,bundle.getInt("hour"),
bundle.getInt("minute"), true);

case DIALOG:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle(R.string.app_name);
alertDialogBuilder.setMessage(mMessage);
alertDialogBuilder.setIcon(R.drawable.ic_launcher);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(
getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int arg1) {

dialog.dismiss();
if (finishActivity == true) {
Activity act = (Activity) mContext;
act.finish();
}

}
});
alertDialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_btn_title), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
CustomDialogFragment.this.dismiss();
}
});


return alertDialogBuilder.create();

}

//Define your custom dialog or alert dialog here and return it.
return new Dialog(getActivity());
}
}

& 这样使用日期选择器

1) 在您的 Activity 或 fragment 中实现 OnDateSetListener(完成后它将返回结果到 onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 方法)

2)像这样从Fragment中调用DatePicker

   CustomDialogFragment dialog = new CustomDialogFragment(YourFragment.this);
Bundle bundle = new Bundle();
bundle.putInt("dialog_id", CustomDialogFragment.DATE_PICKER);
bundle.putInt("year", mYear);
bundle.putInt("month", mMonth);
bundle.putInt("day", mDay);
dialog.setArguments(bundle);
dialog.show(getFragmentManager(), "dialog");

& 像这样从 Activity(或 FragmentActivity)调用 DatePicker

               CustomDialogFragment dialog = new CustomDialogFragment(this);
Bundle bundle = new Bundle();
bundle.putInt("dialog_id", CustomDialogFragment.DATE_PICKER);
bundle.putInt("year", mYear);
bundle.putInt("month", mMonth);
bundle.putInt("day", mDay);
dialog.setArguments(bundle);
dialog.setCancelable(false);
dialog.show(this.getSupportFragmentManager(), "dialog");

& 根据 Set "dialog_id",您可以使用 TIME_PICKER(用于时间选择器对话框)和 DIALOG(用于简单对话框)根据发送 "dialog_id" 根据通过包将数据发送到 CustomDialogFragment

关于java - 如何在 fragment 中实现 Android 日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19720305/

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