0

我在片段中有一个 TimePicker。在创建时,我希望片段将 TimePicker 的当前时间选择设置为 0:00,但它似乎总是希望将其默认设置为当前时间。

如何通过 setCurrentHour/Minute 设置它并确保它保持不变?(我不应该从主机活动中这样做!)

这是我的片段:

public class CreateProfileFragment extends Fragment implements OnClickListener {

   private OnClickListener listener;
   private EditText name;
   private CheckBox sun, mon, tue, wed, thur, fri, sat;
   private TimePicker reportTimePicker;
   private Button createNewProfileButton;

   /**
    * Required interface the host activity must implement to handle click
    * events.
    * 
    * @author Tony Tran
    * 
    */
   public interface OnClickListener {

      public void createNewProfile(UserProfile profile);
   }



   /**
    * Checks to see if the host activity has implemented the required interface.
    */
   public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof OnClickListener) {
         listener = (OnClickListener) activity;
      } else {
         throw new ClassCastException(activity.toString()
               + " must implement CreateProfileFragment.OnClickListener");
      }
   }



   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      View toReturn = inflater.inflate(
            R.layout.fragment_create_new_profile_layout, container, false);
      name = (EditText) toReturn.findViewById(R.id.new_profile_name);
      sun = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_sunday);
      mon = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_monday);
      tue = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_tuesday);
      wed = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_wednesday);
      thur = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_thursday);
      fri = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_friday);
      sat = (CheckBox) toReturn
            .findViewById(R.id.checkbox_create_new_profile_recording_day_saturday);
      reportTimePicker = (TimePicker) toReturn
            .findViewById(R.id.report_time_picker);
      reportTimePicker.setCurrentHour(0);
      reportTimePicker.setCurrentHour(0);
      createNewProfileButton = (Button) toReturn
            .findViewById(R.id.create_new_profile_btn);
      createNewProfileButton.setOnClickListener(this);
      return toReturn;
   }



   /**
    * OnClick handler for this fragment.
    */
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
      case (R.id.create_new_profile_btn):
         generateProfileToReturn();
      }
   }



   /**
    * Retrieves entered information and sends back to host's createNewProfile()
    * method.
    */
   private void generateProfileToReturn() {
      String profileName = name.getText().toString();
      boolean[] loggingDays = { sun.isChecked(), mon.isChecked(),
            tue.isChecked(), wed.isChecked(), thur.isChecked(),
            fri.isChecked(), sat.isChecked() };
      int reportHour = reportTimePicker.getCurrentHour();
      int reportMinute = reportTimePicker.getCurrentMinute();
      UserProfile createdProfile = new UserProfile(profileName, loggingDays,
            reportHour, reportMinute);
      listener.createNewProfile(createdProfile);
   }
}
4

1 回答 1

0

似乎是一个错字,但您在初始化视图时没有设置当前分钟。相反,您将当前时间设置两次:

reportTimePicker.setCurrentHour(0);
reportTimePicker.setCurrentHour(0);

将其中一个更改为setCurrentMinute(0)您应该设置。

于 2013-09-15T11:21:30.317 回答