gpt4 book ai didi

java - 当我有 MyCustomClass 扩展 DialogFragment 时如何实现 onDatePickerChanged

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:01 25 4
gpt4 key购买 nike

我有一个名为 DatePicker 的自定义类,它扩展了 DialogFragment 并实现了 DatePickerDialog.OnDateSetListener,但我的问题是因为您需要监听onDateChangeMethodDialogFragment 只有 DialogInterface.OnCancelListenerDialogInterface.OnDismissListener

我怎样才能做到这一点?我尝试实现 OnDateChangedListener 并重写 onDateChange 方法,但它不起作用。这是代码:

public class DatePicker extends DialogFragment implements
DatePickerDialog.OnDateSetListener, OnDateChangedListener {

private OnDateSetListener _onDateSetListener;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

_onDateSetListener = (OnDateSetListener) activity;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Date initialValue = null;
String[] buttons = getArguments().getStringArray("buttons");

Long initialValueLong = getArguments().getLong("initialValue");
if (initialValue == null) {
initialValue = new Date();
}

Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(initialValueLong);

DatePickerDialog dialog = new DatePickerDialog(getActivity(), this,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE));


return dialog;
}

@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
_onDateSetListener.onDateSet(new GregorianCalendar(year, monthOfYear,
dayOfMonth).getTime(), getArguments().get("tag"));
}

@Override
public void onDateChanged(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

// THIS METHOD NEVER CALLED

}
}

最佳答案

您似乎尚未在类中声明 onDateSetListener,并且您还将主机 Activity 转换为错误的类型。 Dialog 类本身应该是 OnDateSetListener,并且主机 Activity 应该转换为其他内容。

尝试像这样构造类:

public class DatePickerDialogFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener
{
public static interface DatePickedListener
{
public void onDatePicked(int selectedYear, int selectedMonth, int selectedDay);
}

private DatePickedListener listener;

@Override
public void onAttach(Activity activity)
{
// when the fragment is initially attached to the activity, cast the
// activity to the callback interface type
super.onAttach(activity);

try
{
listener = (DatePickedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement " + DatePickedListener.class.getName());
}
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Bundle b = getArguments();

int year = b.getInt("set_year");
int month = b.getInt("set_month");
int day = b.getInt("set_day");

return new DatePickerDialog(getActivity(), this, year, month, day);
}


@Override
public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay)
{
// when the date is selected, send it to the activity via its callback interface method

listener.onDatePicked(setYear, setMonth, setDay);
}
}

关于java - 当我有 MyCustomClass 扩展 DialogFragment 时如何实现 onDatePickerChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738890/

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