gpt4 book ai didi

android - 从 Android 中的 DatePickerFragment 获取日期对象

转载 作者:太空狗 更新时间:2023-10-29 13:53:59 25 4
gpt4 key购买 nike

我在我的 android 应用程序中定义了 DatePickerFragment,这是它的代码:

public class DatePickerFragment extends DialogFragment implements        DatePickerDialog.OnDateSetListener {    private int year;    private int month;    private int day;    @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);    }    @Override    public void onDateSet(DatePicker view, int year, int month, int day) {        this.year = year;        this.month = month;        this.day = day;    }    public Date getDateFromDatePicker(){        Calendar calendar = Calendar.getInstance();        calendar.set(year, month, day);        return calendar.getTime();    }}

I want to retrieve the date from this date picker from TaskFragment, and set the "date" attribute in my class Task to be equal to the date selected

        // Method for date picker to appear once the "Due Date" button is clicked.
datePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
DatePickerFragment datePicker = new DatePickerFragment();
datePicker.show(fm, "datePicker");

task.setDate(datePicker.getDateFromDatePicker());
//test
System.out.println(task.getDate());


}
});

单击按钮时会出现日期选择器。但是,日期设置不正确,实际上是在按下日期按钮后立即设置的,而不是从选择器中选择日期时设置的。此外,在打印日期时,无论我选择什么日期,我都会得到以下信息:I/System.out:12 月 31 日星期三 10:47:38 GMT+00:00 2阅读此处的一些解决方案指导我定义 onDateSet 和 getDateFromDatePicker 方法,但它们似乎无法正常运行。非常感谢任何帮助,谢谢!

最佳答案

经过一番挖掘,我按照我在Android Programming: The Big Nerd Ranch Guide一书中读到的方法,清楚地指定了如何在两个 fragment 之间传递数据。以下是我遵循的步骤:

  1. 设置目标 fragment :在这里,我通过 setTargetFragment 方法将接收日期的 fragment (在我的例子中是 TaskFragment)设为 DatePickerFragment 的目标 fragment


// set taskFragment as the target fragment of DatePickerFragment
datePicker.setTargetFragment(TaskFragment.this, 0);
datePicker.show(fm, "datePicker");

  1. 通过 DatePickerFragment 的 onDateSet 方法将日期发送到目标 Fragment。为此,我将带有日期的 Intent 传递给目标 fragment 的 onActivityResult 方法。


public void onDateSet(DatePicker view, int year, int month, int day) {
// create date object using date set by user
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date date = calendar.getTime();

if (getTargetFragment() == null)
return;

Intent intent = new Intent();
intent.putExtra(DATE, date);

// pass intent to target fragment
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);

  1. 覆盖目标 fragment 中的 onActivityResult 方法以接受日期


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
return;
}

if (requestCode == REQUEST_DATE){
Date date = (Date) data.getSerializableExtra(DatePickerFragment.DATE);
task.setDate(date);

}
}

关于android - 从 Android 中的 DatePickerFragment 获取日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41740048/

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