gpt4 book ai didi

java - 使用 GridView 制作日历

转载 作者:行者123 更新时间:2023-11-30 01:23:39 25 4
gpt4 key购买 nike

我编写了一些代码来使用 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;
}
}

最佳答案

“我想知道是否有更简单的制作日历的方法,或者是否有一种方法可以更改当天 GridView 中单个项目的外观。”

检查这个库:https://github.com/SundeepK/CompactCalendarView

您可以将事件添加到日期,该日期将显示为天数下方的一个小循环。

关于java - 使用 GridView 制作日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com