使用GridView制作日历

时间:2016-04-20 11:35:01

标签: java android gridview calendar

我有一些代码用于显示使用GridView的日历,我想知道是否有更简单的制作日历的方法,或者是否有可以改变外观的方法当天的GridView中的单个项目。代码如下。

TextView tvMonth;
GridView gvCal;
DateFormat dateFormat;
Date date;
public static String[] days;
public static int[] months = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int today, beginOfMonth;
String month, year;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calender);

    gvCal = (GridView) findViewById(R.id.gridView);
    dateFormat = new SimpleDateFormat("yyyy");
    date = new Date();
    months[1] = Feb(Integer.parseInt(dateFormat.format(date))); // Find the amount of days in Feb
    dateFormat = new SimpleDateFormat("MM");
    int numDays = months[Integer.parseInt(dateFormat.format(date))-1] + 6; // Number of days in the month as well as making sure not to override the day names
    // Check which day of the month the month started on. Eg: April 1st 2016 is a Friday
    dateFormat = new SimpleDateFormat("MM");
    month = dateFormat.format(date);
    dateFormat = new SimpleDateFormat("yyyy");
    year = dateFormat.format(date);
    try {
        beginOfMonth = (Day("01"+month+year))-1; // Get the beginning of the month (-1 because Android recognizes Sunday as the first day)
    } catch (ParseException pe) {
        Toast.makeText(getApplicationContext(), pe.getMessage(), Toast.LENGTH_LONG).show();
    }
    if (beginOfMonth == 0) {
        beginOfMonth = 7;
    }
    days = new String[numDays+beginOfMonth];
    days[0] = "Mon";
    days[1] = "Tue";
    days[2] = "Wed";
    days[3] = "Thu";
    days[4] = "Fri";
    days[5] = "Sat";
    days[6] = "Sun";
    dateFormat = new SimpleDateFormat("dd");
    String temp = dateFormat.format(date);
    today = Integer.parseInt(temp);

    if(beginOfMonth != 0) {
        for (int i = 7; i <= (5 + beginOfMonth); i++) {
            days[i] = "";
        }
    }
    for (int i = (6 + beginOfMonth); i <= (days.length-1); i++) {
        days[i] = Integer.toString(i-beginOfMonth-5);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.cal_days, days);
    gvCal.setAdapter(adapter);

    gvCal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

    tvMonth = (TextView) findViewById(R.id.textView);
    dateFormat = new SimpleDateFormat("MMMM"); // Get month name
    tvMonth.setText(dateFormat.format(date));
}

public int Feb(int year) {
    int temp;
    try {
        temp = year / 4;
    } catch (Exception e) {
        return 28;
    }
    return 29;
}

public int Day(String day) throws ParseException {
    DateFormat df = new SimpleDateFormat("ddMMyyyy");
    try {
        Date d = df.parse(String.valueOf(day));
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.DAY_OF_WEEK);
    } catch (Exception e) {
        ParseException pe = new ParseException("There was a problem getting the date.", 0);
        throw pe;
    }
}

1 个答案:

答案 0 :(得分:1)

&#34;我想知道是否有更简单的制作日历的方法,或者是否有一种方法可以改变当前GridView中单个项目的外观。&#34 ;

检查此库:https://github.com/SundeepK/CompactCalendarView

您可以将事件添加到日期,该日期将在一天内显示为一个小周期。