Android日历控件的实现方法
本文实例为大家分享了Android日历控件的实现代码,供大家参考,具体内容如下
1、效果图:
2、弹窗Dialog:SelectDateDialog:
publicclassSelectDateDialog{
privatestaticfinalStringTAG="SelectDateDialog";
privateDialogdialog;
privateTextViewdateText;
privateintselectYear,selectMonth;
privateAppCompatActivitymContext;
privateDateAdapteradapter;
privateListselWeekList=newArrayList<>();
privateListlist=newArrayList<>();
publicSelectDateDialogbuilder(AppCompatActivitymContext,intyear,intmonth){
this.mContext=mContext;
this.selectYear=year;
this.selectMonth=month;
//获取Dialog布局
Viewview=LayoutInflater.from(mContext).inflate(R.layout.dialog_date,null);
//定义Dialog布局和参数
dialog=newDialog(mContext,R.style.AlertDialogStyle);
dialog.setCanceledOnTouchOutside(false);//点击外部是否取消
dialog.setCancelable(false);
dialog.setContentView(view);
Windowwindow=dialog.getWindow();
WindowManager.LayoutParamsparams=window.getAttributes();
params.width=(ScreenUtils.getScreenWidth(mContext));
//params.height=(int)(ScreenUtils.getScreenHeight(mContext)*0.5);
window.setAttributes(params);
window.setGravity(Gravity.BOTTOM);
RecyclerViewrecycler=view.findViewById(R.id.recycler_select_date);
dateText=view.findViewById(R.id.date_text);
dateText.setText(year+"年"+month+"月");
//下个月
view.findViewById(R.id.next_month).setOnClickListener(view13->{
if(selectMonth>11){
selectYear=selectYear+1;
selectMonth=1;
}else{
selectMonth++;
}
showNewData(selectYear,selectMonth);
});
//上个月
view.findViewById(R.id.last_month).setOnClickListener(view14->{
if(selectMonth<2){
selectYear=selectYear-1;
selectMonth=12;
}else{
selectMonth--;
}
showNewData(selectYear,selectMonth);
});
list=DataUtils.getCalendar(year,month);
adapter=newDateAdapter(mContext,list);
GridLayoutManagermanager=newGridLayoutManager(mContext,7);
recycler.setLayoutManager(manager);
recycler.setAdapter(adapter);
//取消
view.findViewById(R.id.middle_cancel).setOnClickListener(view1->{
dialog.dismiss();
});
//确定
view.findViewById(R.id.middle_determine).setOnClickListener(view1->{
initSelect();
});
//设置选中当天
adapter.setNowDay(year,month,DataUtils.getLastMonth(year,month));
returnthis;
}
privatevoidshowNewData(intyear,intmonth){
list=DataUtils.getCalendar(year,month);
//更新月数据
adapter.setList(list);
adapter.setNowDay(year,month,DataUtils.getLastMonth(year,month));
dateText.setText(year+"年"+month+"月");
}
/**
*获取选中的日期
*/
privatevoidinitSelect(){
selWeekList.clear();
for(inti=0;i
ScreenUtils:
publicclassScreenUtils{
publicstaticintgetScreenWidth(Contextcontext){
DisplayMetricslocalDisplayMetrics=newDisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
returnlocalDisplayMetrics.widthPixels;
}
}
DateBean:
publicclassDateBean{
privateintweek;
privatebooleansign;
//0上月1本月2下月
privateintmonth;
privatebooleanisFlag;
publicbooleanisFlag(){
returnisFlag;
}
publicvoidsetFlag(booleanflag){
isFlag=flag;
}
publicintgetWeek(){
returnweek;
}
publicvoidsetWeek(intweek){
this.week=week;
}
publicbooleanisSign(){
returnsign;
}
publicvoidsetSign(booleansign){
this.sign=sign;
}
publicintgetMonth(){
returnmonth;
}
publicvoidsetMonth(intmonth){
this.month=month;
}
publicDateBean(intweek,booleansign,intmonth){
this.week=week;
this.sign=sign;
this.month=month;
}
}
适配器:DateAdapter
publicclassDateAdapterextendsCommonRecyclerAdapter{
privatestaticfinalStringTAG="DateAdapter";
privateIntegernowDay;
privateintyear;
privateintmonth;
publicDateAdapter(Contextcontext,Listlist){
super(context,R.layout.item_date,list);
}
publicvoidsetNowDay(intyear,intmonth,intnowDay){
this.nowDay=nowDay;
this.year=year;
this.month=month;
notifyDataSetChanged();
}
@Override
publicvoidconvert(RecyclerHolderholder,DateBeanitem,intposition){
TextViewnumber=holder.getView(R.id.item_number);
number.setText(item.getWeek()+"");
//当前年月等于切换年月时才选中当天
if(position==nowDay){
Stringdate=year+"-"+month;
if(date.equals(DataUtils.timeInMillsTrasToDate(1))){
number.setBackgroundResource(R.drawable.date_unsel_shape);
number.setTextColor(Color.WHITE);
}else{
number.setTextColor(Color.parseColor("#333333"));
}
}else{
number.setBackgroundResource(0);
number.setTextColor(Color.parseColor("#333333"));
if(item.getMonth()==0||item.getMonth()==2){
number.setTextColor(Color.parseColor("#CDCDCD"));
}else{
number.setTextColor(Color.parseColor("#333333"));
}
}
//点击事件
number.setOnClickListener(view->{
//本月可以点击
intnowYear=Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
intnowMonth=Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
//只有在今天之后的日期才可以选中
if(year==nowYear){
if(item.getMonth()==1&&month==nowMonth&&position>nowDay){
onClick(item,number);
}elseif(month>nowMonth){
onClick(item,number);
}
}elseif(year>nowYear){
onClick(item,number);
}
});
}
privatevoidonClick(DateBeanitem,TextViewnumber){
if(item.isFlag()){
item.setFlag(false);
number.setBackgroundResource(0);
number.setTextColor(Color.parseColor("#333333"));
}else{
item.setFlag(true);
number.setBackgroundResource(R.drawable.date_sel_shape);
number.setTextColor(Color.WHITE);
}
}
}
注意:CommonRecyclerAdapter之前写过,这里不再重复
数据源:DataUtils
publicclassDataUtils{
privatestaticfinalStringTAG="DataUtils";
/**
*日历
*/
publicstaticListgetCalendar(intcurrentYear,intcurrentMonth){
//实例化集合
Listlist=newArrayList<>();
Calendarc=Calendar.getInstance();
c.clear();
/**
*处理上个月月末
*/
if(currentMonth-1==0){
c.set(Calendar.YEAR,currentYear-1);
c.set(Calendar.MONTH,11);
}else{
c.set(Calendar.YEAR,currentYear);
c.set(Calendar.MONTH,(currentMonth-2));
}
intlast_sumDays=c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DATE,last_sumDays);
//得到一号星期几
intweekDay=c.get(Calendar.DAY_OF_WEEK);
if(weekDay<7){
for(inti=weekDay-1;i>=0;i--){
Integerdate=newInteger(last_sumDays-i);
list.add(newDateBean(date,true,0));
}
}
/**
*处理当前月
*/
c.clear();
c.set(Calendar.YEAR,currentYear);
c.set(Calendar.MONTH,currentMonth-1);
intcurrentsumDays=c.getActualMaximum(Calendar.DAY_OF_MONTH);
for(inti=1;i<=currentsumDays;i++){
Integerdate=newInteger(i);
list.add(newDateBean(date,true,1));
}
/**
*处理下个月月初
*/
c.clear();
if(currentMonth==12){
c.set(Calendar.YEAR,currentYear+1);
c.set(Calendar.MONTH,0);
}else{
c.set(Calendar.YEAR,currentYear);
c.set(Calendar.MONTH,currentMonth);
}
c.set(Calendar.DATE,1);
intcurrentWeekDay=c.get(Calendar.DAY_OF_WEEK);
if(currentWeekDay>2&¤tWeekDay<8){
for(inti=1;i<=8-currentWeekDay;i++){
list.add(newDateBean(i,true,2));
}
}
returnlist;
}
/**
*获取上个月大小
*/
publicstaticIntegergetLastMonth(intcurrentYear,intcurrentMonth){
//实例化集合
Listlist=newArrayList<>();
Calendarc=Calendar.getInstance();
c.clear();
/**
*处理上个月月末
*/
if(currentMonth-1==0){
c.set(Calendar.YEAR,currentYear-1);
c.set(Calendar.MONTH,11);
}else{
c.set(Calendar.YEAR,currentYear);
c.set(Calendar.MONTH,(currentMonth-2));
}
intlast_sumDays=c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DATE,last_sumDays);
//得到一号星期几
intweekDay=c.get(Calendar.DAY_OF_WEEK);
if(weekDay<7){
for(inti=weekDay-1;i>=0;i--){
list.add(i);
}
}
Log.e(TAG,"getLastMonth:"+Integer.parseInt(timeInMillsTrasToDate(0)));
returnlist.size()+Integer.parseInt(timeInMillsTrasToDate(0))-1;
}
/**
*list转string字符串
*/
publicstaticStringreturnList(Listlist){
if(null==list&&list.size()==0){
return"";
}else{
//去除空格
Stringstr=String.valueOf(list).replaceAll("","");
returnstr.substring(1,str.length()-1);
}
}
/**
*时间转换
*/
@TargetApi(Build.VERSION_CODES.N)
publicstaticStringtimeInMillsTrasToDate(intformatType){
DateFormatformatter=null;
switch(formatType){
case0:
formatter=newSimpleDateFormat("dd");
break;
case1:
formatter=newSimpleDateFormat("yyyy-MM");
break;
case2:
formatter=newSimpleDateFormat("yyyy");
break;
case3:
formatter=newSimpleDateFormat("MM");
break;
}
Calendarcalendar=Calendar.getInstance();
returnformatter.format(calendar.getTime());
}
}
使用:DateActivity
publicclassDateActivityextendsAppCompatActivity{
privateButtonopenDate;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
openDate=this.findViewById(R.id.open_date);
openDate.setOnClickListener(view->{
showDateDialog();
});
}
privatevoidshowDateDialog(){
intyear=Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
intmonth=Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
newSelectDateDialog().builder(this,year,month)
.setListener(selectDate->openDate.setText(selectDate)).show();
}
}
对应布局:activity_date
2、资源文件:
date_sel_shape.xml
date_unsel_shape.xml
dialog_shape.xml
item_date.xml:
颜色:
#008577
#00574B
#D81B60
样式:
@android:color/transparent
@null
true
@null
true
true
true
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。