赞
踩
今天写一个从代码中添加系统日历提醒的功能,(分两篇,一个添加到系统日历,一个从手机上能直接查看)
这个是项目中的日历以及日历事件展示
项目里面用的这个日历就是第三方的日历了,看了一下,里面的功能以及样式还是很全面的,还有一些比较细节的功能,比如点击左上角日期可以查看全部月份
//日历
implementation 'com.haibin:calendarview:3.7.1'
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.github.tntkhang.ui.home.ReturnVisit_Avtivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_77sdp"
android:background="#fff"
android:orientation="horizontal">
<ImageView
android:id="@+id/gather_back"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginTop="@dimen/_30sdp"
android:padding="@dimen/_15sdp"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:text="回访记录"
android:textColor="#333333"
android:textSize="16dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#F1F1F1" />
</RelativeLayout>
<LinearLayout
android:layout_marginTop="@dimen/_77sdp"
android:id="@+id/ll_slect_mon"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/monday_text"
android:paddingVertical="10dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textColor="#000000"
android:text="4月10日"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:gravity="center"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/year_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2022"/>
<TextView
android:id="@+id/nong_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
</LinearLayout>
</LinearLayout>
<com.haibin.calendarview.CalendarLayout
android:layout_below="@+id/ll_slect_mon"
android:id="@+id/calendarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
app:calendar_content_view_id="@+id/recyclerView">
<com.haibin.calendarview.CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:calendar_padding="10dp"
app:current_month_lunar_text_color="#CFCFCF"
app:current_month_text_color="#333333"
app:max_year="2020"
app:min_year="2004"
app:month_view="com.github.tntkhang.utils.CustomMonthView"
app:month_view_show_mode="mode_fix"
app:other_month_lunar_text_color="#e1e1e1"
app:other_month_text_color="#e1e1e1"
app:scheme_text="假"
app:scheme_text_color="#333"
app:scheme_theme_color="#128c4b"
app:selected_lunar_text_color="#fff"
app:selected_text_color="#fff"
app:selected_theme_color="#2A97FF"
app:week_background="#fff"
app:week_start_with="sun"
app:week_text_color="#e1e1e1"
app:week_view="com.github.tntkhang.utils.CustomWeekView"
app:year_view="com.github.tntkhang.utils.CustomYearView"
app:year_view_day_text_color="#333333"
app:year_view_day_text_size="7sp"
app:year_view_month_text_color="#1887D5"
app:year_view_month_text_size="20sp"
app:year_view_scheme_color="#f17706"
app:year_view_select_text_color="#fff"
app:year_view_week_height="12dp"
app:year_view_week_text_color="#e1e1e1"
app:year_view_week_text_size="6sp" />
</com.haibin.calendarview.CalendarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/calendarLayout">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.tntkhang.adapter.Calen_Adapter;
import com.github.tntkhang.bean.CalenDateBean;
import com.github.tntkhang.bean.CalenDateListBean;
import com.github.tntkhang.mvp.Constant22;
import com.github.tntkhang.mvp.base.BaseActivity22;
import com.github.tntkhang.utils.SPUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import vn.nextlogix.tntkhang.R;
public class ReturnVisit_Avtivity extends BaseActivity22 implements CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener, View.OnClickListener {
@BindView(R.id.gather_back)
ImageView gather_back;
@BindView(R.id.ll_slect_mon)
LinearLayout ll_slect_mon;
@BindView(R.id.monday_text)
TextView monday_text;
@BindView(R.id.year_text)
TextView year_text;
@BindView(R.id.nong_text)
TextView nong_text;
@BindView(R.id.calendarLayout)
CalendarLayout calendarLayout;
@BindView(R.id.calendarView)
CalendarView calendarView;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@Override
protected int Layout() {
return R.layout.activity_return_visit_avtivity;
}
@Override
protected void initView(Bundle savedInstanceState) {
calendarView.setOnCalendarSelectListener(this);
calendarView.setOnYearChangeListener(this);
//点击事件
gather_back.setOnClickListener(this);
monday_text.setOnClickListener(this);
//设置月日时间
monday_text.setText(new SimpleDateFormat("MM月dd日").format(new Date().getTime()));
//进入页面默认查询一边回访数据
Map<String,Object> map=new HashMap<>();
map.put("userId", SPUtils.getStringFromSP("userid"));
map.put("date",new SimpleDateFormat("yyyy-MM-dd").format(new Date().getTime()));
mPresenter.startgetInfoHavaToken(Constant22.BASE_Return_List, CalenDateListBean.class,map,"");
//进入页面后去回访日历数据
Map<String,Object> map_date=new HashMap<>();
map_date.put("userId", SPUtils.getStringFromSP("userid"));
mPresenter.startgetInfoHavaToken(Constant22.BASE_Return_List, CalenDateBean.class,map_date,"");
LinearLayoutManager manager=new LinearLayoutManager(this);
manager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(manager);
}
@Override
protected void startCoding() {
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.gather_back:
finish();
break;
case R.id.monday_text:
if (!calendarLayout.isExpand()) {
calendarLayout.expand();
return;
}
calendarView.showYearSelectLayout(2022);
monday_text.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onSuccess(Object o) {
if(o instanceof CalenDateListBean){
Calen_Adapter calen_adapter = new Calen_Adapter(((CalenDateListBean) o).getData(),this);
recyclerView.setAdapter(calen_adapter);
}
if(o instanceof CalenDateBean){
Map<String, Calendar> map_calen = new HashMap<>();
for (int i = 0; i < ((CalenDateBean) o).getData().size(); i++) {
map_calen.put(getSchemeCalendar( ((CalenDateBean) o).getData().get(i).getYear(), ((CalenDateBean) o).getData().get(i).getMonth(), ((CalenDateBean) o).getData().get(i).getDay(), 0xFF40db25, "访").toString(),
getSchemeCalendar( ((CalenDateBean) o).getData().get(i).getYear(), ((CalenDateBean) o).getData().get(i).getMonth(), ((CalenDateBean) o).getData().get(i).getDay(), 0xFF40db25, "访"));
}
//此方法在巨大的数据量上不影响遍历性能,推荐使用
calendarView.setSchemeDate(map_calen);
}
}
@Override
public void onError(String error) {
}
@Override
public void onCalendarOutOfRange(Calendar calendar) {
}
@Override
public void onCalendarSelect(Calendar calendar, boolean isClick) {
monday_text.setText(calendar.getMonth()+"月"+calendar.getDay()+"日");
year_text.setText(calendar.getYear()+"");
nong_text.setText(calendar.getLunar()+"");
Map<String,Object> map=new HashMap<>();
map.put("userId", SPUtils.getStringFromSP("userid"));
map.put("date",calendar.getYear()+"-"+calendar.getMonth()+"-"+calendar.getDay());
mPresenter.startgetInfoHavaToken(Constant22.BASE_Return_List, CalenDateListBean.class,map,"");
}
@Override
public void onYearChange(int year) {
}
private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
Calendar calendar = new Calendar();
calendar.setYear(year);
calendar.setMonth(month);
calendar.setDay(day);
calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
calendar.setScheme(text);
calendar.addScheme(new Calendar.Scheme());
calendar.addScheme(0xFF008800, " ");
calendar.addScheme(0xFF008800, " ");
return calendar;
}
}
这个第三方日历总的来说是很好用的,可以自定义每个添加日历提醒的独立样式,在点击的时候也有很多交互,点击日历查看当天添加的事件,这个地方是根据项目需求和后端配合的,向后端传递日期数据,拿到出参后用展示出来就好了,随后在添加一下列表的点击等操作,就能够实现一个完美的项目日历模块了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。