private void onTimeButtonClicked() { mTimeLayout = (LinearLayout)mLayoutInflater.inflate(R.layout.time_dialog, null); mTimePicker = (TimePicker)mTimeLayout.findViewById(R.id.time_picker); mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub mToDoItemInfo.setTime_hour(hourOfDay); mToDoItemInfo.setTime_minute(minute); mTimeString = mToDoItemInfo.getTimeString(); } }); new AlertDialog.Builder(mContext) .setTitle(getResources().getString(R.string.time_dialog_title)) .setView(mTimeLayout) .setPositiveButton(getResources().getString(R.string.time_dialog_save_button_default_text), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub if(mTimeString == null){ mTimeString = String.format("%02d:%02d", Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE)); } mTimeButton.setText(mTimeString); } }) .setNegativeButton(getResources().getString(R.string.time_dialog_cancel_button_default_text), null) .show(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm_app); // init(); // private void init(){ mTimePicker = (TimePicker) findViewById(R.id.timePicker1); set = (Button) findViewById(R.id.set); cancel = (Button) findViewById(R.id.cancel); //mTitleTv = (TextView) findViewById(R.id.showTime); btn_back = (Button)findViewById(R.id.btn_back); c.setTimeInMillis(System.currentTimeMillis()); // �x�a���s��ť set.setOnClickListener(this); cancel.setOnClickListener(this); btn_back.setOnClickListener(this); // �]�m24�p�ɨ� mTimePicker.setIs24HourView(false); mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { AlarmApp.setHour = hourOfDay; AlarmApp.setMinute = minute; // int setHour=c.get(Calendar.HOUR_OF_DAY); // int setMinute=c.get(Calendar.MINUTE); } }); }
private void createTime(final int _type, Activity _activity, final long _maxDate, final long _minDate) { this.timePicker = new TimePicker(_activity); this.timePicker.setIs24HourView(true); this.timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); this.timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE)); calendar.setTimeInMillis(_maxDate); final int _maxHour = calendar.get(Calendar.HOUR_OF_DAY); final int _maxMinute = calendar.get(Calendar.MINUTE); calendar.setTimeInMillis(_minDate); final int _minHour = calendar.get(Calendar.HOUR_OF_DAY); final int _minMinute = calendar.get(Calendar.MINUTE); calendar.setTimeInMillis(System.currentTimeMillis()); final String _maxStr = String.format(Locale.getDefault(), "%d-%02d-%02d %02d:%02d:00", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), _maxHour, _maxMinute); final String _minStr = String.format(Locale.getDefault(), "%d-%02d-%02d %02d:%02d:00", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), _minHour, _minMinute); this.timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { if (_type == 0) { if (_maxDate > _minDate) { String _val = String.format(Locale.getDefault(), "%d-%02d-%02d %02d:%02d:00", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth(), hourOfDay, minute); long _currentTime = getTime(_val); if (_currentTime > _maxDate) { // 当前值大于最大值 calendar.setTimeInMillis(_maxDate); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), null); timePicker.setCurrentHour(_maxHour); timePicker.setCurrentMinute(_maxMinute); } else if (_currentTime < _minDate) { // 当前值小于最小值 calendar.setTimeInMillis(_minDate); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), null); timePicker.setCurrentHour(_minHour); timePicker.setCurrentMinute(_minMinute); } } } else { if (getTime(_maxStr) > getTime(_minStr)) { if (hourOfDay < _minHour) { timePicker.setCurrentHour(_minHour); } if (hourOfDay > _maxHour) { timePicker.setCurrentHour(_maxHour); } if (hourOfDay == _minHour) { // 如果当前选中时间的小时 = // 最小值,那么就比较分钟 if (minute < _minMinute) { timePicker.setCurrentMinute(_minMinute); } } if (hourOfDay == _maxHour) { // 如果当前选中时间的小时 = // 最大值,那么就比较分钟 if (minute > _maxMinute) { timePicker.setCurrentMinute(_maxMinute); } } } } } }); }