gpt4 book ai didi

android - 将值传递给 TimePickerFragment

转载 作者:行者123 更新时间:2023-11-30 03:18:31 26 4
gpt4 key购买 nike

我有一个可用的 TimePickerFragment,它使用当前时间作为选择器的默认值。现在我想传递分钟和小时的旧选定值以显示在选择器中。我该怎么做?

这是代码:

Activity :

    TimePickerFragment newFragmentNight = TimePickerFragment.newInstance(TO_TIME_PICKER_ID);    
newFragmentNight.show(getSupportFragmentManager(), "timePicker");

对话框 fragment :

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{

private int mId;
private TimePickedListener mListener;

static TimePickerFragment newInstance(int id) {
Bundle args = new Bundle();
args.putInt("picker_id", id);
TimePickerFragment fragment = new TimePickerFragment();
fragment.setArguments(args);
return fragment;
}

public Dialog onCreateDialog(Bundle savedInstanceState)
{
// here I don't want to use the current time, I want to use the time passing from the activity
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

mId = getArguments().getInt("picker_id");


// create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}

最佳答案

尝试向 newInstance() 静态方法添加新值并以这种方式 bundle 它。它可以像使用构造函数一样传递信息。尝试以下操作:

static TimePickerFragment newInstance(int id, int hour, int minute) 
{
Bundle args = new Bundle();
args.putInt("picker_id", id);
args.putInt("hour", hour);
args.putInt("minute", minute);
TimePickerFragment fragment = new TimePickerFragment();
fragment.setArguments(args);
return fragment;
}

public Dialog onCreateDialog(Bundle savedInstanceState)
{
Bundle args = getArguments();

final Calendar c = Calendar.getInstance();

//The second input is a default value in case hour or minute are empty
int hour = args.getInt("hour", c.get(Calendar.HOUR_OF_DAY));
int minute = args.getInt("minute", c.get(Calendar.MINUTE));

mId = getArguments().getInt("picker_id");


// create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

希望这对您有所帮助。祝你好运!

关于android - 将值传递给 TimePickerFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19594475/

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