のため申し訳ありませんが、次のそれが示す時間? これはあなたのために働く可能性があります
MainActivity.java
// class implements TimePickerDialog.OnTimeSetListener and uses onTimeSet()
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
//myTime wrapped in Long instead of primitive long so it can be null
Long myTime;
int hours, mins;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//TimePickerDialog.OnTimeSetListener returns here
@Override
public void onTimeSet(TimePicker view, int hour, int min) {
myTime = System.currentTimeMillis();
this.hours = hour;
this.mins = min;
}
//our xml button onClick method to trigger TimePicker dialog
public void timeButtonClicked(View v){
//send bundle arguments to fragment if hour/minute previously set
TimePickerFragment fragment = TimePickerFragment.newInstance(myTime, hours, mins);
fragment.show(getFragmentManager(), "timePicker");
}
}
TimePickerFragment.java
public class TimePickerFragment extends DialogFragment {
public static TimePickerFragment newInstance(Long savedTime, int hours, int mins){
TimePickerFragment myFragment = new TimePickerFragment();
Bundle args = new Bundle();
//savedTime is Long wrapped so it can be null if not set
//and thus bundle will not be set if savedTime is null
if(savedTime!=null){
args.putLong("savedTime", savedTime);
args.putInt("hours", hours);
args.putInt("mins", mins);
myFragment.setArguments(args);
}
return myFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
Long savedTime;
int hoursDiff=0, minsDiff=0;
int hourOfDay, minute;
// create a Calendar instance of the current time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
// create hourOfDay, minute variables to pass as parameters
// to the TimePicker dialog. If passed by bundle, use that
// time and difference otherwise use current time
Bundle bundle = getArguments(); //NOT savedInstanceState bundle
if (bundle != null) {
// unix time in milliseconds since Epoch set
// by System.currentTimeMillis() by MainActivity.onTimeSet()
savedTime = bundle.getLong("savedTime");
// set calendar to passed savedTime + (current time difference)
long timeDifference = currentTime-savedTime;
minsDiff = (int)(timeDifference/60000);
if(minsDiff>60) hoursDiff = (int)(minsDiff/60);
// hours and mins last set by MainActivity.onTimeSet()
hourOfDay=bundle.getInt("hours")+hoursDiff;
minute=bundle.getInt("mins")+minsDiff;
}
else {
// bundle not set, use current time
hourOfDay = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
}
// Create a new instance of TimePickerDialog with hourOfDay and minute parameters and return the fragment
return new TimePickerDialog(getActivity(),
(TimePickerDialog.OnTimeSetListener) getActivity(), hourOfDay, minute,
DateFormat.is24HourFormat(getActivity()));
}
}