4

我有 2 个 TextView,我可以从中调用时间选择器。但我无法弄清楚我从哪个 TextBox 中调用时间选择器。以下是来自 Activity 的代码片段:

case R.id.tv_to_night:
     // show the time picker dialog
     TimePickerFragment newFragmentNight = new TimePickerFragment();
     newFragmentNight.show(getSupportFragmentManager(), "timePicker to");

     break;
case R.id.tv_from_night:

     // show the time picker dialog
     TimePickerFragment newFragment = new TimePickerFragment();
     newFragment.show(getSupportFragmentManager(), "timePicker from");

     break;

在这里,我想知道我从哪个 TextBox 获得时间:

public void onTimePicked(Calendar time) {
        if(depending from were it was called)
            tv_from_night.setText(DateFormat.format("h:mm a", time));
        else
            tv_to_night.setText(DateFormat.format("h:mm a", time));
    }

时间选择器片段

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {
    private TimePickedListener mListener;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

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

    @Override
    public void onAttach(Activity activity) {
        // when the fragment is initially shown (i.e. attached to the activity),
        // cast the activity to the callback interface type
        super.onAttach(activity);
        try {
            mListener = (TimePickedListener) activity;
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement " + TimePickedListener.class.getName());
        }
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // when the time is selected, send it to the activity via its callback
        // interface method
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);

        mListener.onTimePicked(c);
    }

    public static interface TimePickedListener {
        public void onTimePicked(Calendar time);
    }
}
4

1 回答 1

2

解决方案

活动:

case R.id.tv_to_night:           
    TimePickerFragment newFragmentNight = TimePickerFragment.newInstance(TO_TIME_PICKER_ID);
    newFragmentNight.show(getSupportFragmentManager(), "timePicker");
    break;
case R.id.tv_from_night:                    
    TimePickerFragment newFragment = TimePickerFragment.newInstance(FROM_TIME_PICKER_ID);
    newFragment.show(getSupportFragmentManager(), "timePicker");
    break;

在这里,您可以获得带有 id 的时间:

public void onTimePicked(Calendar time, int id) {

    Log.i("TimePicker", "Time picker called from id " + id);

    switch(id) {

    case FROM_TIME_PICKER_ID:
        // do thomething

        break;

    case TO_TIME_PICKER_ID:
        // do thomething
        break;
    }

}

时间选择器片段:

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    private int mId;
    private TimePickedListener mListener;

    static TimePickerFragment newInstance(int id) {
        Bundle args = new Bundle();
        args.putInt("picker_id", id);
        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        mId = getArguments().getInt("picker_id");

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

    @Override
    public void onAttach(Activity activity) {
        // when the fragment is initially shown (i.e. attached to the activity),
        // cast the activity to the callback interface type
        super.onAttach(activity);
        try {
            mListener = (TimePickedListener) activity;
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement " + TimePickedListener.class.getName());
        }
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // when the time is selected, send it to the activity via its callback
        // interface method
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);

        if(mListener != null)
            mListener.onTimePicked(c, mId);
    }

    public static interface TimePickerDialogListener {
        public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute);
    }

    public static interface TimePickedListener {
        public void onTimePicked(Calendar time, int id);
    }
}
于 2013-09-22T15:57:11.593 回答