gpt4 book ai didi

java - 在日历页面更改监听器 Android 上获取日历月份

转载 作者:行者123 更新时间:2023-12-02 01:33:50 24 4
gpt4 key购买 nike

我正在创建一个使用 applandeo 的 Material 日历 View 库的小型应用程序。

每当用户单击前进或上一步按钮时,我都会尝试获取当前月份和年份。

    public class CalendarActivity extends AppCompatActivity {

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

CalendarView calendarView = findViewById(R.id.calendarView);

//Move to another Activity
calendarView.setOnDayClickListener(eventDay -> {
// code to move to another activity
//...
});

calendarView.setOnForwardPageChangeListener(new OnCalendarPageChangeListener() {
@Override
public void onChange() {
showToast();
}
});


calendarView.setOnPreviousPageChangeListener(new OnCalendarPageChangeListener() {
@Override
public void onChange() {
showToast();
}
});
}

private void showToast(){
// set month and year here....
Toast.makeText(getApplicationContext(),"current month " + month + " current year " + year, Toast.LENGTH_SHORT).show();
}
}

有没有办法获取当前显示的月份和年份?

如有任何帮助,我们将不胜感激。

最佳答案

enter image description here

应用程序 Gradle:

implementation 'com.applandeo:material-calendar-view:1.7.0'
implementation 'com.google.code.gson:gson:2.8.6'

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.applandeo.materialcalendarview.CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

片段:

public class CustomCalendarFragment extends Fragment {

private ArrayList<EmployeeLeave> mLeaves = new ArrayList<>();
private List<EventDay> mEvents = new ArrayList<>();
private EmployeeViewModel mViewModel;
private CalendarView mCalendarView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_customer_calendar_view, container, false);

CalendarView mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
mCalendarView.setHeaderColor(R.color.colorSubraTamarillo);
mCalendarView.setHeaderLabelColor(R.color.colorWhite);
mCalendarView.setForwardButtonImage(getResources().getDrawable(R.drawable.ic_arrow_right));
mCalendarView.setPreviousButtonImage(getResources().getDrawable(R.drawable.ic_arrow_left));

Calendar mCalendar = Calendar.getInstance();
getLeaveListByDate(getFirstDate(Calendar.DAY_OF_MONTH, mCalendar), getLastDate(Calendar.DAY_OF_MONTH, mCalendar));

//-----------------------------------------------| Previous Month
mCalendarView.setOnPreviousPageChangeListener(new OnCalendarPageChangeListener() {
@Override
public void onChange() {
mCalendar.set(mCalendar.MONTH, mCalendar.get( mCalendar.MONTH ) - 1);
getLeaveListByDate(getFirstDate(mCalendar.DAY_OF_MONTH, mCalendar), getLastDate(mCalendar.DAY_OF_MONTH, mCalendar));
}
});
//-----------------------------------------------| Forward Month
mCalendarView.setOnForwardPageChangeListener(new OnCalendarPageChangeListener() {
@Override
public void onChange() {
mCalendar.set(mCalendar.MONTH, mCalendar.get( mCalendar.MONTH ) + 1);
getLeaveListByDate(getFirstDate(mCalendar.DAY_OF_MONTH, mCalendar), getLastDate(mCalendar.DAY_OF_MONTH, mCalendar));
}
});

return view;
}

//=================================================| Get start date and end date from current month
private String getFirstDate(int dayOfMonth, Calendar cal) {
cal.set(dayOfMonth, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); //(Month, 1)
return getInstance().getFormatFromStringDate("yyyy-MM-dd", cal.getTime());
}

private String getLastDate(int dayOfMonth, Calendar cal) {
cal.set(dayOfMonth, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); //(Month, 31)
return getInstance().getFormatFromStringDate("yyyy-MM-dd", cal.getTime());
}

public String getFormatFromStringDate(String pattern, Date date) {
return new java.text.SimpleDateFormat(pattern).format(date); //java.text.DateFormat.getDateTimeInstance().format(ts.getTime())
}

//=================================================| Creates event from employee list
private void getLeaveListByDate(String fromDate, String toDate) {
String json = "[{\"empList\":[{\"department\":\"Sewing\",\"designation\":\"Sr. Manager\",\"eid\":\"1004\",\"FirstName\":\"Mohammad Shahabul Alam\",\"Gender\":\"Male\",\"LastName\":\"\",\"leaveDate\":\"2020-03-02T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"},{\"department\":\"Sewing \",\"designation\":\"Helper\",\"eid\":\"0011\",\"FirstName\":\"al harbi Accounting Manager\",\"Gender\":\"Female\",\"LastName\":\"\",\"leaveDate\":\"2020-03-02T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"}],\"leaveDate\":\"2020-03-02T00:00:00\",\"totalEmployee\":2},{\"empList\":[{\"department\":\"Sewing\",\"designation\":\"Jr. Operator\",\"eid\":\"00021\",\"FirstName\":\"tanver\",\"Gender\":\"Male\",\"LastName\":\"rana\",\"leaveDate\":\"2020-03-03T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"}],\"leaveDate\":\"2020-03-03T00:00:00\",\"totalEmployee\":1},{\"empList\":[{\"department\":\"Sewing\",\"designation\":\"Sr. Manager\",\"eid\":\"1004\",\"FirstName\":\"Mohammad Shahabul Alam\",\"Gender\":\"Male\",\"LastName\":\"\",\"leaveDate\":\"2020-03-04T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"}],\"leaveDate\":\"2020-03-04T00:00:00\",\"totalEmployee\":1},{\"empList\":[{\"department\":\"Sewing \",\"designation\":\"Helper\",\"eid\":\"0011\",\"FirstName\":\"al harbi Accounting Manager\",\"Gender\":\"Female\",\"LastName\":\"\",\"leaveDate\":\"2020-03-05T00:00:00\",\"MobileNo\":\"\",\"status\":\"CL\"},{\"department\":\"Admin\",\"designation\":\"Asst. Manager\",\"eid\":\"00100\",\"FirstName\":\"shek jamal\",\"Gender\":\"Male\",\"LastName\":\"\",\"leaveDate\":\"2020-03-05T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"}],\"leaveDate\":\"2020-03-05T00:00:00\",\"totalEmployee\":2},{\"empList\":[{\"department\":\"Sewing \",\"designation\":\"Helper\",\"eid\":\"0011\",\"FirstName\":\"al harbi Accounting Manager\",\"Gender\":\"Female\",\"LastName\":\"\",\"leaveDate\":\"2020-03-07T00:00:00\",\"MobileNo\":\"\",\"status\":\"SL\"}],\"leaveDate\":\"2020-03-07T00:00:00\",\"totalEmployee\":1}]";

ArrayList<EmployeeLeave> arrayList = new Gson().fromJson(json, new TypeToken<ArrayList<EmployeeLeave>>(){}.getType());
mLeaves.addAll(arrayList);
for (EmployeeLeave leave : arrayList) {
Calendar cal = getDayFromString(Calendar.getInstance(), leave.getLeaveDate());
//mEvents.add(new EventDay(cal, CalendarUtils.getDrawableText(getActivity(), "" + leave.getTotalEmployee(), null, R.color.colorSubraTamarillo, 12)));
mEvents.add(new EventDay(cal, getCircleDrawableWithText(getActivity(), "" + leave.getTotalEmployee())));
}
if (mEvents != null) {
mCalendarView.setEvents(mEvents);
}
}

//=================================================| Create circle with text drawable
private Drawable getCircleDrawableWithText(Context context, String string) {
Drawable background = ContextCompat.getDrawable(context, R.drawable.sample_circle);
Drawable text = CalendarUtils.getDrawableText(context, string, null, android.R.color.white, 12);
Drawable[] layers = {background, text};
return new LayerDrawable(layers);
}

}

关于java - 在日历页面更改监听器 Android 上获取日历月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com