下面列出了怎么用android.widget.CalendarView的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
database=new DataBase(this.getContext());
cal_doj=getActivity().findViewById(R.id.calview_doj);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
data_date = sdf.format(new Date(cal_doj.getDate()));
cal_doj.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int date) {
if(month==12)
month=1;
else
month=month+1;
data_date=date + "/" + month + "/" + year;
Toast.makeText(getContext(), date + "/" + month + "/" + year , Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendarView = (CalendarView) findViewById(R.id.CalendarView); // get the reference of CalendarView
calendarView.setDate(System.currentTimeMillis(), false, true);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Intent intent = new Intent(MainActivity.this, OverviewActivity.class);
// Build string from chosen date to parse into Date object
// (month+1) because months count from 0 in java but SimpleDateFormat parses it as 1-12
String chosenDate = dayOfMonth + "/" + (month+1) + "/" + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = sdf.parse(chosenDate);
long millis = date.getTime();
intent.putExtra("DATE", millis);
} catch (ParseException e){
e.printStackTrace();
}
startActivity(intent);
}
});
overridePendingTransition(0, 0);
}
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
textView.setText(FORMATTER.format(view.getDate()));
}
public CalendarView getCalendarView() {
return calendarView;
}
@Override
public CalendarView getCalendarView() {
return mCalendarView;
}
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
textView.setText(FORMATTER.format(view.getDate()));
}
public static BaseDSL.ViewClassResult calendarView() {
return BaseDSL.v(CalendarView.class);
}
public static Void calendarView(Anvil.Renderable r) {
return BaseDSL.v(CalendarView.class, r);
}
public static Void onDateChange(CalendarView.OnDateChangeListener arg) {
return BaseDSL.attr("onDateChange", arg);
}
public static BaseDSL.ViewClassResult calendarView() {
return BaseDSL.v(CalendarView.class);
}
public static Void calendarView(Anvil.Renderable r) {
return BaseDSL.v(CalendarView.class, r);
}
public static Void onDateChange(CalendarView.OnDateChangeListener arg) {
return BaseDSL.attr("onDateChange", arg);
}
public void onDateChanged(CalendarView v, int y, int m, int d) {
dateInMillis = new GregorianCalendar(y, m, d).getTimeInMillis();
}
@Override
public void onSelectedDayChange(
CalendarView view, int year, int month,
int dayOfMonth) {
textView.setText(FORMATTER.format(view.getDate()));
}
public CalendarViewAssert(CalendarView actual) {
super(actual, CalendarViewAssert.class);
}
/**
* Gets the {@link CalendarView}.
* <p>
* This method returns {@code null} when the
* {@link android.R.attr#datePickerMode} attribute is set
* to {@code calendar}.
*
* @return The calendar view.
* @see #getCalendarViewShown()
*/
public CalendarView getCalendarView() {
return mDelegate.getCalendarView();
}
CalendarView getCalendarView();