Date and time picker in Sketchware
1. Create a new project in Sketchware.
2. Add a Button button1 for picking date and time.
3. Add a TextView textview1, for displaying the selected date and time.
4. Add a Calendar component cal.
5. Create two more blocks, pickDate and pickTime.
6. In pickDate more block, use an add source directly block and put following code:
DatePickerDialog datePicker = new DatePickerDialog(
this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
_pickTime();
}
},
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
);
datePicker.show();
7. In pickTime more block, use an add source directly block and put following code:
TimePickerDialog timePicker = new TimePickerDialog(
this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
textview1.setText(new SimpleDateFormat("dd/MM/yyyy hh:mm a").format(cal.getTime()));
}
},
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true
);
timePicker.show();
8. In button1 onClick event, use the pickDate more block.
9. Save and run the project.
Comments
Post a Comment