Android: Control representing date

1. Date control

(1), DatePicker

        DatePicker is a commonly used control on the Android platform, used to allow users to select dates. It is usually displayed as a calendar and allows the user to select the year, month and day by swiping or tapping.

Common properties:

  • android:calendarViewShown: Whether to display the calendar view, the default is true. If set to false, only the scroll selector for year, month and day will be displayed.
  • android:calendarTextColor  : The color of the text of the calendar list
  • android:calendarViewShown : Whether to display the calendar view
  • android:datePickerMode : component appearance, optional values: spinner, calendar. The effect of the former is as follows, and the default effect is the latter.
  • android:calendarTextColor  : The color of the text of the calendar list
  • android:calendarViewShown : Whether to display the calendar view
  • android:datePickerMode : component appearance, optional values: spinner, calendar. The former effect is as follows, the default effect is the latter

Commonly used methods:

  • init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener): Initialize the DatePicker control and set the selected date and date change listener.
  • getYear(): Get the selected year.
  • getMonth(): Get the selected month (range is 0-11).
  • getDayOfMonth(): Get the selected date.
  • setMinDate(long minDate): Set the minimum date to limit the earliest date selected by the user.
  • setMaxDate(long maxDate): Set the maximum date to limit the latest date selected by the user.

(2) CalendarView 

        CalendarView is an Android control used to display calendars and select dates. It provides an interactive interface that allows users to browse different months and select specific dates.

Common properties

  • android:date: Specify the default selected date, expressed in YYYY-MM-DD format.
  • android:minDate: Specifies the minimum value of the optional date, expressed in YYYY-MM-DD format.
  • android:maxDate: Specify the maximum value of the optional date, expressed in YYYY-MM-DD format.
  • android:weekNumberColor: Set the color of week number text.
  • android:focusedMonthDateColor: Set the color of the date text in the current month.
  • android:unfocusedMonthDateColor: Set the color of date text in non-current months.
  • android:selectedWeekBackgroundColor: Set the color of the week background of the selected date.
  • android:selectedDateVerticalBar: Set the color of the vertical bar on the left side of the selected date.
  • android:shownWeekCount: Set the number of weeks displayed in the calendar.

Common methods

  • setDate(long milliseconds): Set the default selected date.
  • getDate(): Get the currently selected date and return a long type representing the milliseconds of the date.
  • setMinDate(long minDate): Set the minimum value of selectable dates, that is, limit the earliest date that users can select.
  • getMinDate(): Get the minimum value of the optional date.
  • setMaxDate(long maxDate): Set the maximum value of selectable dates, that is, limit the latest date that users can select.
  • getMaxDate(): Get the maximum value of optional dates.
  • setOnDateChangeListener(OnDateChangeListener listener): Set a date change listener, which is triggered when the user selects a different date.

2. Examples

 MainActivity :

public class MainActivity extends AppCompatActivity {

    private CalendarView calendarView;
    private DatePicker datePicker;
    private TextView selectedDateTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendarView = findViewById(R.id.calendarView);
        datePicker = findViewById(R.id.datePicker);
        selectedDateTextView = findViewById(R.id.selectedDateTextView);

        // 设置CalendarView的日期改变监听器
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                // 处理日期改变事件
                String selectedDate = year + "-" + (month + 1) + "-" + dayOfMonth;
                selectedDateTextView.setText("选中日期:" + selectedDate);
            }
        });

        // 设置DatePicker的日期改变监听器
        datePicker.init(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
                new DatePicker.OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 处理日期改变事件
                        String selectedDate = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                        selectedDateTextView.setText("选中日期:" + selectedDate);
                    }
                });
        
        // 初始时显示当前日期
        updateSelectedDate();
    }

    private void updateSelectedDate() {
        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int dayOfMonth = datePicker.getDayOfMonth();
        String selectedDate = year + "-" + (month + 1) + "-" + dayOfMonth;
        selectedDateTextView.setText("选中日期:" + selectedDate);
    }
}

activity_main: 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/selectedDateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="18sp" />

</LinearLayout>

operation result:

Guess you like

Origin blog.csdn.net/A125679880/article/details/131773693