標籤:
可選只選擇日期,也可以同時選擇時間
只選擇日期的情況
同時選擇日期和時間的情況
關鍵代碼:
findViewById(R.id.selectDateButton).setOnClickListener(new OnClickListener() {public void onClick(View v) {View timepickerview=LayoutInflater.from(getContext()).inflate(R.layout.timepicker, null);ScreenInfo screenInfo = new ScreenInfo(getContext());wheelMain = new WheelMain(timepickerview,hasTime);wheelMain.screenheight = screenInfo.getHeight();String time = itemValue.getText().toString();Calendar calendar = Calendar.getInstance();if(JudgeDate.isDate(time, formatStr)){try {calendar.setTime(dateFormat.parse(time));} catch (ParseException e) {e.printStackTrace();}}int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day = calendar.get(Calendar.DAY_OF_MONTH);int hour = calendar.get(Calendar.HOUR_OF_DAY);int min = calendar.get(Calendar.MINUTE);if(hasTime)wheelMain.initDateTimePicker(year,month,day,hour,min);elsewheelMain.initDateTimePicker(year,month,day);new AlertDialog.Builder(context).setTitle("選擇時間").setView(timepickerview).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {itemValue.setText(wheelMain.getTime());}}).setNegativeButton("取消", null).show();}});WheelMain wheelMain = new WheelMain(timepickerview,hasTime);if(hasTime)wheelMain.initDateTimePicker(year,month,day,hour,min);elsewheelMain.initDateTimePicker(year,month,day);
代碼:這裡
Android 仿iPhone的日期時間選取器