简体   繁体   中英

How to get the time of the TimePickerDialog in the EditText in another fragment?

I am trying to get the time of my TimePickerDialog to an EditText in the FinishPostFragment .

I am showing the selected time right now in a Toast but how can I show the data in the EditText in the FinishPostFragment ?

FinishPostFragment in my onCreateView:

timeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // view TimePickerDialog
            showTimeDialog(v);
        }
    });

FinishPostFragment outside my onCreateView:

public void showTimeDialog(View v){
    TimeDialogFragment tdf = new TimeDialogFragment();
    tdf.show(getFragmentManager().beginTransaction(), "TimePickerFragment");
}

TimePickerDialog.OnTimeSetListener test = new TimePickerDialog.OnTimeSetListener(){
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        time_finish.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
    }
};

TimeSettings.java:

public class TimeSettings implements TimePickerDialog.OnTimeSetListener {
    Context context;

    public TimeSettings(Context context){
        this.context = context;
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(context, "Selected time is hour: " +hourOfDay+ " minute: " + minute, Toast.LENGTH_LONG).show();
    }
}

TimeDialogFragment:

public class TimeDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        Calendar calendar = Calendar.getInstance();

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

        TimePickerDialog dialog;

        TimeSettings timeSettings = new TimeSettings(getActivity());
        dialog = new TimePickerDialog(getActivity(), timeSettings, hour, minute,
                android.text.format.DateFormat.is24HourFormat(getActivity()));

        return dialog;
    }
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Toast.makeText(context, "Selected time is hour: " +hourOfDay+ " minute: " + minute, Toast.LENGTH_LONG).show();
String time = "   "+hourString+"h"+minuteString+"m"+secondString+"s";
    yourTextView.setText(time);

} 

try this settting text to your textview like this.

Something close to what you have that might work is the following. Have your FinishPostFragment implement TimePickerDialog.OnTimeSetListener itself instead.

Show the TimeDialaogFragment using getChildFragmentManager() instead.

tdf.show(getChildFragmentManager().beginTransaction(), "TimePickerFragment");

That will make it so you can then do the following in your TimeDialogFragment.onCreateView to send the selected time directly to the FinishPostFragment .

TimePickerDialog.OnTimeSetListener timeSetListener = 
        (TimePickerDialog.OnTimeSetListener) getParentFragment();

dialog = new TimePickerDialog(getActivity(), timeSetListener, hour, minute,
        android.text.format.DateFormat.is24HourFormat(getActivity()));

Note in order to use getParentFragment() you must be using the support library or targeting API 17+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM