我选择时间戳之后,如何让日历运行时间?如果我选择,似乎日历的时间不会运行。如果我选择下午1:00,它将永远是下午1:00。我如何在1分钟后1:01 PM制作它?我做的是在timepicker之后,我将它设置为日历。
抱歉我的英文
答案 0 :(得分:0)
如果我正确理解您的问题,您希望用户能够使用TimePicker对话框设置时钟,并让TimePicker对话框显示上次设置的时间+下次显示的已用时间? 这可能对您有用:
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()));
}
}