刚刚才实现简单的显示日历,用gridview实现界面设计,比较简陋啊~~555,无奈实在是水平低级,等待逐步提升了。
实现内容:
1、显示日期信息
2、可变化月份
3、点击日期可以显示日记
4、双击日期或者双击日记备注的显示部分,可以跳转至日记编辑
5、添加关于备注的内容
6、每页日历需要有默认当前日,选中或者默认当前日需要有特定的背景色
7、加入设置功能
待实现内容:
特殊的日子要有特殊的颜色
记录代码,以免忘记。代码有误,概不负责。
package PrivateDiary.Activity;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
public class CanlenderActivity extends Activity{
Calendar ical = Calendar.getInstance();
int currentYear = ical.get(Calendar.YEAR);//保存当前信息
int currentMonth = ical.get(Calendar.MONTH);
int currentDay = ical.get(Calendar.DAY_OF_MONTH);
TextView CurrentDayView;
Button NextMonthBtn,PreMonthBtn;
GridView gridview;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_main);
gridview = (GridView)findViewById(R.id.gridview_main);
//添加元素给GridView
GridAdapter gadapter = new GridAdapter(this, currentYear,currentMonth);
gridview.setAdapter(gadapter);
CurrentDayView = (TextView)findViewById(R.id.CurrentDayText);
updataDataView(currentYear,currentMonth);
NextMonthBtn = (Button)findViewById(R.id.NextMonthBtn);
PreMonthBtn = (Button)findViewById(R.id.PreMonthBtn);
NextMonthBtn.setOnClickListener(new NextMonthBtn_Listener());
PreMonthBtn.setOnClickListener(new PreMonthBtn_Listener());
}
//显示下一个月的信息
private class NextMonthBtn_Listener implements OnClickListener{
@Override
public void onClick(View v) {
if(currentMonth == 11)
{
currentMonth = 0;
currentYear++;
}else{
currentMonth++;
}
gridview.setAdapter(new GridAdapter(CanlenderActivity.this,currentYear,currentMonth));
updataDataView(currentYear,currentMonth);
}
}
//显示前一个月的信息
private class PreMonthBtn_Listener implements OnClickListener{
@Override
public void onClick(View v) {
if(currentMonth == 0)
{
currentYear--;
currentMonth = 11;
}else{
currentMonth--;
}
gridview.setAdapter(new GridAdapter(CanlenderActivity.this, currentYear, currentMonth));
updataDataView(currentYear,currentMonth);
}
}
//刷新标题日期
private void updataDataView(int year, int month){
month++;
CurrentDayView.setText(year+"年"+month+"月");
}
public class GridAdapter extends BaseAdapter{
int week;
int monthDays;
Calendar cal = Calendar.getInstance();
//定义Context
private Context m_context;
private String[] number = new String[42];
public GridAdapter(Context c, int currentYear, int currentMonth){
m_context = c;
getweek(currentYear,currentMonth);
}
@Override
//获取个数
public int getCount() {
return number.length;
}
//获取位置
@Override
public Object getItem(int position) {
return position;
}
//获取ID
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
//第一屏
convertView = LayoutInflater.from(m_context).inflate(R.layout.gridview_text, null);
}
TextView textview = (TextView)convertView.findViewById(R.id.gv_text);
String strpos = number[position];
textview.setText(strpos);
if((ical.get(Calendar.MONTH)==cal.get(Calendar.MONTH)&&ical.get(Calendar.YEAR)==cal.get(Calendar.YEAR))&&(position == currentDay+7+week))
{//当天信息颜色变化
textview.setBackgroundColor(R.color.Gray1);
}
if((position<=monthDays+week+7&&position>week+7)||position<7){
//添加颜色信息
textview.setTextColor(R.color.black);
}
return convertView;
}
//获得天数
private int getMonthDays(int year, int month){
month++;//注意此处需要将月份加1
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:{
return 31;
}
case 4:
case 6:
case 9:
case 11:{
return 30;
}
case 2:{
if(((year%4==0)&&(year%100 != 0))||(year%400==0))
return 29;
else
return 28;
}
}
return 0;
}
private void getweek(int year, int month){
//将当前日历设为置顶月份的第一天
cal.set(year,month,1);
//获得指定月份的第1天使当前周的第几天
week = cal.get(Calendar.DAY_OF_WEEK)-2;
System.out.println("week="+week+"DAY_OF_WEEK="+cal.get(Calendar.DAY_OF_WEEK));
monthDays = getMonthDays(year, month);
int lastmonthDays = getMonthDays(year, month-1);//上月
number[0]="周日";
number[1]="周一";
number[2]="周二";
number[3]="周三";
number[4]="周四";
number[5]="周五";
number[6]="周六";
for(int i=0; i<number.length-7; i++){
//周一
if(i<=week){//上月显示
int temp = lastmonthDays - week;
number[i+7] = (temp +i)+"";
}else if(i <= monthDays+week){//当前月显示
number[i+7]=i-week+"";
}else{//下个月显示
for(int j=0;j<number.length-i;j++){
int temp=number.length-monthDays-week;
number[i+7]=temp-(j+1)+"";
}
}
}
}
}
}
下面是xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity = "center"
android:padding = "1dip"
android:id = "@+id/gridtextback"
>
<TextView
android:id = "@+id/gv_text"
android:layout_height = "fill_parent"
android:layout_width = "fill_parent"
android:textSize = "36px"
android:gravity = "center"
android:background = "@color/white"
android:textColor = "@color/gray"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical"
android:paddingLeft = "50dip"
android:paddingRight = "50dip"
android:background = "@color/BackGreen">
<LinearLayout android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:orientation = "horizontal"
android:gravity = "center_vertical">
<Button
android:id = "@+id/PreMonthBtn"
android:background = "@drawable/prev_month"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
/>
<TextView
android:id = "@+id/CurrentDayText"
android:layout_width = "wrap_content"
android:layout_weight = "1"
android:layout_height = "wrap_content"
android:gravity = "center"
android:textColor = "@color/black"
android:textSize = "36px"/>
<Button
android:id = "@+id/NextMonthBtn"
android:background = "@drawable/next_month"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:layout_alignParentRight="true"/>
</LinearLayout>
<GridView
android:id = "@+id/gridview_main"
android:background="#A9A9A9"
android:padding="1.0dip"
android:scrollbars="none"
android:fadingEdge="none"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1.0dip"
android:verticalSpacing="1.0dip"
android:numColumns="7"
android:layout_centerHorizontal="true"
/>
</LinearLayout>