Loading timePickerDialog from a fragment

我只是一个虾纸丫 提交于 2019-11-29 15:27:47

Hey first of all you need to remove the onCreateView of your timepicker fragment. I also could find in example share with me.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {...}

So your TimerPickerFragment should look like below

package com.ravit.android.aroundtheclock;

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

Caution: If your app supports versions of Android lower than 3.0, be sure that you call getSupportFragmentManager() to acquire an instance of FragmentManager. Also make sure that your activity that displays the time picker extends FragmentActivity instead of the standard Activity class.

mButtonEnd = (Button)v.findViewById(R.id.end_workday);
mButtonEnd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        DialogFragment newFragment  = new TimePickerFragment();
        //newFragment.show(getActivity().getFragmentManager(), DIALOG_TIME);
        newFragment.show(getSupportFragmentManager(), DIALOG_TIME);
        // if you are using the nested fragment then user the 
        //newFragment.show(getChildFragmentManager(), DIALOG_TIME);
    }
});

Let me know if you have any question. thanks

Here is a complete example, which can be used either from Fragment and Activity:

public class TimePickerDialogFragment extends DialogFragment {

    private static final String ARGUMENT_HOUR = "ARGUMENT_HOUR";
    private static final String ARGUMENT_MINUTE = "ARGUMENT_MINUTE";
    private static final String ARGUMENT_IS_24_HOURS = "ARGUMENT_IS_24_HOURS";
    private TimePickerDialog.OnTimeSetListener listener;

    private int hourOfDay;
    private int minute;
    private boolean is24Hours;

    public static TimePickerDialogFragment newInstance(final int hour, final int minute, final boolean is24Hours) {
        final TimePickerDialogFragment df = new TimePickerDialogFragment();
        final Bundle args = new Bundle();
        args.putInt(ARGUMENT_HOUR, hour);
        args.putInt(ARGUMENT_MINUTE, minute);
        args.putBoolean(ARGUMENT_IS_24_HOURS, is24Hours);
        df.setArguments(args);
        return df;
    }

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        retrieveArguments();
    }

    private void retrieveArguments() {
        final Bundle args = getArguments();
        if (args != null) {
            hourOfDay = args.getInt(ARGUMENT_HOUR);
            minute = args.getInt(ARGUMENT_MINUTE);
            is24Hours = args.getBoolean(ARGUMENT_IS_24_HOURS);
        }
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        return new TimePickerDialog(getContext(), this.listener, this.hourOfDay, this.minute, this.is24Hours);
    }

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

Initialize and show it:

final Calendar c = Calendar.getInstance();
final boolean is24Hours = DateFormat.is24HourFormat(getContext());
final TimePickerDialogFragment timePicker = TimePickerDialogFragment.newInstance(
        c.get(Calendar.HOUR_OF_DAY),
        c.get(Calendar.MINUTE),
        is24Hours);
timePicker.setListener(this);
timePicker.showNow(getChildFragmentManager(), null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!