Android TimePicker不会返回数据

时间:2016-03-28 08:37:07

标签: android timepicker

我现在对Android非常陌生并且正在学习。

我正在尝试实施TimePicker。一切正常,直到用户必须选择时间,然后特定时间不会更新。

我做错了什么?可以在GitHub上找到rest of my code

public class TimePickerFragment extends DialogFragment {
  private static final String ARG_TIME = "date";
  public static final String EXTRA_TIME = "time";
  private TimePicker mTimePicker;

  public static TimePickerFragment newInstance(Date date) {

    Bundle args = new Bundle();
    args.putSerializable(ARG_TIME, date);

    TimePickerFragment fragment = new TimePickerFragment();
    fragment.setArguments(args);
    return fragment;
  }


  public Dialog onCreateDialog(Bundle savedInstanceState) {

    View v = LayoutInflater.from(getActivity()).inflate(R.layout.picker_time_fragment, null);
    final Date time = (Date) getArguments().getSerializable(ARG_TIME);

    Calendar a = Calendar.getInstance();
    final int hour = a.get(Calendar.HOUR_OF_DAY);
    final int minute = a.get(Calendar.MINUTE);
    mTimePicker = (TimePicker) v.findViewById(R.id.time_picker_layout);


    return new AlertDialog.Builder(getActivity())
        .setView(v)
        .setTitle(R.string.time_picker_crime_title)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              mTimePicker.setHour(hour);
              mTimePicker.setMinute(minute);
            }

            sendResultTime(Activity.RESULT_OK, time);
          }
        })
        .show();
  }

  private void sendResultTime(int resultCode, Date time) {
    if (getTargetFragment() == null) {
      return;
    }
    Intent intent = new Intent();
    intent.putExtra(EXTRA_TIME, time);

    getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
  }

}

3 个答案:

答案 0 :(得分:0)

// create the real bean
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(-1);
messageSource.setBasenames("classpath:conf-test/messages"); // this folder is just for testing and it contains a messages_en.properties file

// inject the bean into the class I wanted to test
ReflectionTestUtils.setField(service, "messageSource", messageSource);

与timePicker相关的代码。这可能有所帮助。忽略Zainbow DB线。这只是验证特定实体的时间。不需要任何东西。

答案 1 :(得分:0)

尝试使用

mTimePicker.setCurrentHour(hour); mTimePicker.setCurrentMinute(minute);

而不是:

mTimePicker.setHour(hour); mTimePicker.setMinute(minute);

并删除该sdk条件......

答案 2 :(得分:0)

为什么不使用TimePickerDialog?我确定你只是希望用户选择正确的时间吗?这就是你需要的:

TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
              @Override
              public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                //User selected something           
              }
        }, 9, 11, true);
timePicker.show();