片段类

时间:2018-05-02 04:59:46

标签: android classcastexception timepicker

我可以实施

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

3 个答案:

答案 0 :(得分:3)

您可以将侦听器设置为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.
    }

}

答案 1 :(得分:2)

按照以下代码: 传递活动或片段上下文。

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();
    }

答案 2 :(得分:2)

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

警告:如果您的应用支持的Android版本低于3.0,请确保调用getSupportFragmentManager()以获取FragmentManager的实例。还要确保显示时间选择器的活动扩展了FragmentActivity而不是标准的Activity类。