当前位置:  首页  >  PHP资讯  >  业界资讯

Android使用GridLayout绘制自定义日历控件

这篇文章主要为大家详细介绍了Android使用GridLayout绘制自定义日历控件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

效果图

思路:就是先设置Gridlayout的行列数,然后往里面放置一定数目的自定义日历按钮控件,最后实现日历逻辑就可以了。

步骤:

第一步:自定义日历控件(初步)

第二步:实现自定义单个日期按钮控件

第三步:将第二步得到的控件动态添加到第一步的布局中,并实现日期逻辑

第四步:编写单个日期点击监听器接口

第一步:自定义日历控件(初步)

 <&#63;xml version="1.0" encoding="utf-8"&#63;>             <--!自定义的单个日历按钮控件就放在这里-->       
 package com.包名.myCalendarView; import java.util.Calendar; import java.util.Date; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.GridLayout; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import com.xuy849.utils.Info; import com.xuy849.weightapp.R; public class MyCalendar extends LinearLayout implements OnClickListener{ Context context; TextView tv_yearAndMonth; ImageButton ib_pre; ImageButton ib_next; ImageButton ib_back; GridLayout gl_calendar; View view; CalendarButton buttons[]; int sideLength; String week[] = {"日","一","二","三","四","五","六"}; Date date; Calendar calendar; int year,month,day; int res; public MyCalendar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public MyCalendar(Context context, AttributeSet attrs) { super(context, attrs); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public MyCalendar(Context context) { super(context); this.cOntext= context; // TODO Auto-generated constructor stub init(); } private void init(){ this.view = LayoutInflater.from(context).inflate(R.layout.my_calendar_view, this); } } 

效果图:(请无视ListView)

第二步:自定义单个日期按钮控件

 <&#63;xml version="1.0" encoding="utf-8"&#63;>      

 package com.包名.myCalendarView; import java.util.Date; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.xuy849.utils.Info; import com.xuy849.weightapp.R; class CalendarButton extends LinearLayout{ Context context; TextView tv_date; TextView tv_data; ImageView iv_note; LinearLayout ll_container; View view; Date date; public CalendarButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public CalendarButton(Context context, AttributeSet attrs) { super(context, attrs); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public void setTotalEnable(boolean b){ this.setEnabled(b); } public CalendarButton(Context context) { super(context); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public void setDate(Date date){ this.date = date; } public Date getDate(){ return this.date; } private void init(){ view = LayoutInflater.from(context).inflate(R.layout.date_button, this); //findViewById tv_date =(TextView)view.findViewById(R.id.textView1); tv_data = (TextView)view.findViewById(R.id.textView2); iv_note = (ImageView)view.findViewById(R.id.imageView1); ll_cOntainer= (LinearLayout)view.findViewById(R.id.linearLayout01); } public void setDateText(String text){ tv_date.setText(text); } public void setDateTextColor(int color){ tv_date.setTextColor(color); } } } 

效果图:

第三步:将第二步得到的控件在java代码中添加到第一步的布局中,并添加相关逻辑

 package com.包名.myCalendarView; import java.util.Calendar; import java.util.Date; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.GridLayout; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import com.xuy849.utils.Info; import com.xuy849.weightapp.R; public class MyCalendar extends LinearLayout implements OnClickListener{ Context context; TextView tv_yearAndMonth; ImageButton ib_pre; ImageButton ib_next; ImageButton ib_back; GridLayout gl_calendar; View view; CalendarButton buttons[]; int sideLength; String week[] = {"日","一","二","三","四","五","六"}; Date date; Calendar calendar; int year,month,day; int res; public MyCalendar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public MyCalendar(Context context, AttributeSet attrs) { super(context, attrs); this.cOntext= context; // TODO Auto-generated constructor stub init(); } public MyCalendar(Context context) { super(context); this.cOntext= context; // TODO Auto-generated constructor stub init(); } private void init(){ this.view = LayoutInflater.from(context).inflate(R.layout.my_calendar_view, this); WindowManager ww = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE); sideLength = ww.getDefaultDisplay().getWidth()/Info.COLUMN_COUNT; // myFindViewById(); //初始化日历按钮布局 initCalendarLayout(); //初始化按钮点击事件 initButton(); } private void initButton(){ ib_pre.setOnClickListener(this); ib_next.setOnClickListener(this); ib_back.setOnClickListener(this); } private void myFindViewById(){ tv_yearAndMOnth= (TextView)view.findViewById(R.id.textView1); ib_pre = (ImageButton)view.findViewById(R.id.imageButton2); ib_next = (ImageButton)view.findViewById(R.id.imageButton3); ib_back = (ImageButton)view.findViewById(R.id.imageButton1); gl_calendar =(GridLayout)view.findViewById(R.id.gridLayout01); // buttOns= new CalendarButton[Info.COLUMN_COUNT*Info.ROW_COUNT]; } //根据传递过来的calendar,绘制当月的日历视图 private void initCalendar(Calendar calendar){ int year = calendar.get(Calendar.YEAR); int mOnth= (calendar.get(Calendar.MONTH)+1); int date = calendar.get(Calendar.DATE); //设置标题 String todayStr = String.format("%04d年%02d月", calendar.get(Calendar.YEAR),(calendar.get(Calendar.MONTH)+1)); tv_yearAndMonth.setText(todayStr); // calendar.set(Calendar.DAY_OF_MONTH, 1); int currentMOnthFirstDateInWeek= calendar.get(Calendar.DAY_OF_WEEK)-1; calendar.set(Calendar.DAY_OF_MONTH, date); int currentMOnthDaysSum= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.roll(Calendar.MONTH, -1); int lastMOnthDaysSum= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int i; calendar.roll(Calendar.MONTH, 1); Log.w("月", currentMonthDaysSum+"天"); Log.w("上月", lastMonthDaysSum +"天"); /* * 设置日期 */ //设置本月 Log.w("currentMonthFirstDateInWeek",currentMonthFirstDateInWeek+""); Log.w("currentMonthFirstDateInWeek%7111",currentMonthDaysSum+currentMonthFirstDateInWeek%7+""); for(i=currentMonthFirstDateInWeek%7+7;i<=(currentMonthDaysSum+currentMonthFirstDateInWeek%7+7-1)&&i 

第四步:编写单个日期点击监听器接口

在第三步中添加方法:

 //设置下标是index的日期按钮的点击事件监听器 public void setOnClickButtonListener(OnClickListener l,int index){ buttons[index].setOnClickListener(l); } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4