我的代码仅在单击一次时更新日历。再次Button
单击时它不会更新日历。Button
我想每周更新日期。下面是我的代码和 logcat,
public class myCalendar extends Activity {
public final static String[] monthcalender = { "January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
public final static int daysinmonths[] = { 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31 };
int mon, yr;
int j=0;
Calendar todaycldr;
// myCalendar moncldr = new myCalendar();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
Button button1 = (Button) findViewById(R.id.button1);
// if (mon == 2) {
// displayMonth(Integer.parseInt(args[0]) - 1,
// Integer.parseInt(args[1]));
// } else {
todaycldr = Calendar.getInstance();
displayMonth(todaycldr.get(Calendar.MONTH), todaycldr.get(Calendar.YEAR));
// }
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
j=7;
displayMonth(todaycldr.get(Calendar.MONTH),
todaycldr.get(Calendar.YEAR));
}
});
}
private void displayMonth(int month, int year) {
// The number of days to leave blank at
// the start of this month.
int blankdays = 0;
System.out.println(" " + monthcalender[month] + " " + year);
if (month < 0 || month > 11) {
throw new IllegalArgumentException("Month " + month
+ " is not valid and must lie in between 0 and
11");
}
GregorianCalendar cldr = new GregorianCalendar(year, month, 1);
System.out
.println("Sunday Monday Tuesday Wednesday Thursday Friday
Saturday");
// Compute how much to leave before before the first day of the month.
// getDay() returns 0 for Sunday.
blankdays = cldr.get(Calendar.DAY_OF_WEEK) - 1;
int daysInMonth = daysinmonths[month];
if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) && month == 1) {
++daysInMonth;
}
// Blank out the labels before 1st day of the month
for (int i = 0; i < blankdays; i++) {
System.out.print(" ");
}
for (int i = 1; i <= daysInMonth; i++) {
// This "if" statement is simpler than messing with NumberFormat
if (i <= 9) {
System.out.print(" ");
}
System.out.print(i+j);
if ((blankdays + i) % 7 == 0) { // Wrap if EOL
System.out.println();
} else {
System.out.print(" ");
}
if (i % 7 ==0)
{
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_calendar, menu);
return true;
}
}
日志猫
09-04 01:00:37.727: I/System.out(591): September 2013
: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
1 2 3 4 5 6 7
September 2013
System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday
System.out(591): 8 9 10 11 12 13 14
I/System.out(591): September 2013
: I/System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday
I/System.out(591): 8 9 10 11 12 13 14
I/System.out(591): September 2013
I/System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday
09-04 01:01:20.017: I/System.out(591): 8 9 10 11 12 13 14
任何建议将不胜感激。谢谢。