片段中的 TimePickerDialog Class

TimePickerDialog in a Fragment Class

我可以实现吗

TimePickerDialog.OnTimeSetListener

片段中 class?

如果可能的话怎么做?

这是我的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()));
}
}

我是否需要修改此代码以便在片段中使用它class?

这是我要实现的地方

public class BluetoothFragment extends Fragment

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

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

您可以将侦听器设置为您的 DialogFragment 以在原始片段

中获得回调
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;
    }
}

这就是您如何在原始片段中获得回调 -

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.
    }

}

遵循以下代码: 传递 Activity 或片段上下文 .

public static void setTimeFromTimePicker(Context context, EditText editText) {

        int hourOfDay, minute;

        final Calendar c = Calendar.getInstance();
        hourOfDay = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        TimePickerDialog dpd = new TimePickerDialog(context, (timePicker, hourOfDay1, minute1) -> {

            Time time = new Time(hourOfDay1, minute1, 0);

            SimpleDateFormat simpleDateFormat = new 
            SimpleDateFormat("hh:mm aa", Locale.getDefault());
            String s = simpleDateFormat.format(time);
            editText.setText(s);
        }, hourOfDay, minute, false);

        dpd.show();
    }
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 */
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {


    public TimePickerFragment() {
        // Required empty public constructor
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker

        final Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

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

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

注意:如果您的应用程序支持低于 3.0 的 Android 版本,请务必调用 getSupportFragmentManager() 来获取 FragmentManager 的实例。还要确保显示时间选择器的 activity 扩展 FragmentActivity 而不是标准的 Activity class.