从DialogFragments

时间:2015-11-16 20:10:29

标签: java android android-dialogfragment android-timepicker

我在活动中有一个输入表单,我想要检索事件的开始时间和结束时间。我正在使用对话框片段中显示的时间戳来获取开始时间并将其返回到主活动,但是如何重新使用相同的逻辑来检索事件的结束时间?我的代码:

主要活动:

public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
private int pickerHour = 0;
private int pickerMin = 0;

//code ommitted for length

public void showTimePickerDialog(View v) {
    TimePickerFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String amOrPm = "am";
    pickerHour = hourOfDay;
    pickerMin = minute;
    String minutesString = "";
    if (hourOfDay > 12){ //translate to 12 hr clock. Little workaround to get both timepicker and am/pm label to show up correctly.
                amOrPm = "pm";
                pickerHour-=12;
            }
            if (pickerMin < 10) {
                minutesString = "0"; //fix weird bug where only one zero is shown on times ending in :00
            }
    startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
}

TimePickerFragment:

public class TimePickerFragment extends DialogFragment {
int hour,minute;
private Activity mActivity;
private TimePickerDialog.OnTimeSetListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
    try {
        mListener = (TimePickerDialog.OnTimeSetListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(mActivity, mListener, hour, minute,
            DateFormat.is24HourFormat(mActivity));
}

}

我需要能够区分开始时间和结束时间,但我不确定如何区分它们而不重复自己。我是Java和Android的新手。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这需要您能够从TimePicker

中检索TimePickerFragment

您需要在创建TimePicker时获取TimePickerFragment个实例,并将其存储在startTimePickerfinishTimePicker这两个字段中。

然后当TimePicker通过onTimeSet传入时,请检查一下TimePicker是什么样的。

if ( timePicker == startTimePicker ) {
      startTimeTextView.setText....
} else {
      finishTimeTextView.setText...
}

答案 1 :(得分:0)

可能像布尔一样简单吗?

public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
    ...
    boolean isSettingStartTime = true;


    //code ommitted for length

    public void showTimePickerDialog(View v) {
        TimePickerFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        ...

        if (isSettingStartTime) {
            startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
            isSettingStartTime = false;
        } else {
            endTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
        }
    }
}