Fragment 内に 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);
}
}