本文介绍如何使用TimeSquare库创建CalenderPickerView 。我们已经看到在许多应用程序中使用CalenderPickerView选择日期。借助该库,我们可以轻松地在我们的应用程序中添加日历。
方法:
- 在build.gradle文件中添加支持库,并在“依赖项”部分中添加依赖项。该库提供内置的日历小部件和各种功能,例如选择特定的日期等。
dependencies { implementation 'com.squareup:android-times-square:1.6.5@aar' }
- 现在,将以下代码添加到activity_main.xml文件中。这将在应用程序中添加CalendarPickerView布局。
activity_main.xml
MainActivity.java
package org.geeksforgeeks.gfgCalenderPickerView; import androidx.appcompat .app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.squareup .timessquare .CalendarPickerView; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class MainActivity extends AppCompatActivity { @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // This will return us today date Date today = new Date(); Calendar nextYear = Calendar.getInstance(); // This will help us // to show date from // today to next year nextYear.add(Calendar.YEAR, 1); CalendarPickerView datePicker = findViewById( R.id.calendar); // we have to initialize // our calender picker view // so we select min date as today // max date as next year // we call getTime() method // because we want to // retrieve date from it. datePicker .init(today, nextYear.getTime()) .inMode(CalendarPickerView .SelectionMode .RANGE) .withSelectedDate(today); // when the user select // or un select any date then // this method called automatically. datePicker .setOnDateSelectedListener( new CalendarPickerView .OnDateSelectedListener() { @Override public void onDateSelected(Date date) { // we have to format our date // object that's why we are // using DateFormat class. String selectedDate = DateFormat .getDateInstance( DateFormat.FULL) .format(date); Toast .makeText( MainActivity.this, selectedDate, Toast.LENGTH_SHORT) .show(); } @Override public void onDateUnselected( Date date) { } }); } }
- 现在,在MainActivity中添加以下代码。 Java文件。这将显示从当前日期算起的下一年的日历。将setOnDateSelectedListener添加到日历中,该日历在用户单击任何日期时被调用。该函数在屏幕上烘烤选定的日期。
主要活动。Java
package org.geeksforgeeks.gfgCalenderPickerView; import androidx.appcompat .app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.squareup .timessquare .CalendarPickerView; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class MainActivity extends AppCompatActivity { @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // This will return us today date Date today = new Date(); Calendar nextYear = Calendar.getInstance(); // This will help us // to show date from // today to next year nextYear.add(Calendar.YEAR, 1); CalendarPickerView datePicker = findViewById( R.id.calendar); // we have to initialize // our calender picker view // so we select min date as today // max date as next year // we call getTime() method // because we want to // retrieve date from it. datePicker .init(today, nextYear.getTime()) .inMode(CalendarPickerView .SelectionMode .RANGE) .withSelectedDate(today); // when the user select // or un select any date then // this method called automatically. datePicker .setOnDateSelectedListener( new CalendarPickerView .OnDateSelectedListener() { @Override public void onDateSelected(Date date) { // we have to format our date // object that's why we are // using DateFormat class. String selectedDate = DateFormat .getDateInstance( DateFormat.FULL) .format(date); Toast .makeText( MainActivity.this, selectedDate, Toast.LENGTH_SHORT) .show(); } @Override public void onDateUnselected( Date date) { } }); } }
输出: