我在片段中有一个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);
}
}
答案 0 :(得分:0)
看起来像是一个错字,但是在初始化视图时你没有设置当前分钟。相反,您要将当前小时设置两次:
reportTimePicker.setCurrentHour(0);
reportTimePicker.setCurrentHour(0);
将其中一个更改为setCurrentMinute(0)
,您应该设置。