gpt4 book ai didi

android - fragment 类中的 TimePickerDialog

转载 作者:太空狗 更新时间:2023-10-29 15:58:36 26 4
gpt4 key购买 nike

我可以实现吗

TimePickerDialog.OnTimeSetListener

fragment 类中?

如果可能怎么做?

这是我的TimePickerFragment.class

public class TimePickerFragment extends DialogFragment {


@NonNull
@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int cHour = c.get(Calendar.HOUR_OF_DAY);
int cMinute = c.get(Calendar.MINUTE);

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), cHour, cMinute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}

我是否需要修改此代码才能在 Fragment 类中使用它?

这就是我要实现它的地方

public class BluetoothFragment extends Fragment

我已经试过了但是我收到了这个错误

java.lang.ClassCastException: com.example.android.bluetoothplantdroid.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener

最佳答案

您可以将 Listener 设置为您的 DialogFragment 以在原始 fragment 中获取回调

public class TimePickerFragment extends DialogFragment {

public static TimePickerFragment newInstance() {
return new TimePickerFragment();
}

private TimePickerDialog.OnTimeSetListener listener;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int cHour = c.get(Calendar.HOUR_OF_DAY);
int cMinute = c.get(Calendar.MINUTE);

return new TimePickerDialog(getActivity(), listener, cHour, cMinute,
android.text.format.DateFormat.is24HourFormat(getActivity()));
}

public void setListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}
}

这就是你如何在原始 fragment 中获得回调 -

public class MyFragment extends Fragment implements TimePickerDialog.OnTimeSetListener {

May be click event of some widget... {
TimePickerFragment.newInstance().setListener(this);
// Show your dialog
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// data when time has been set.
}

}

关于android - fragment 类中的 TimePickerDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127423/

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