如何自定义日历以仅打印当前日期行?

时间:2013-07-28 00:48:40

标签: android

如何只打印那个有当前日期的行?下面是我的代码,它使用2维数组并打印文本视图中的所有值我想过滤该数组并只显示具有当前月份日期的数组我将如何做?下面是函数CalculateCalendar(),它在Hoyahcalencer类对象a中设置数组值 HoyahCalendar.a [i / 7] [i%7]我如何只打印那个当前有效的行我也在下面用cpaste完整代码进行参考

  public void CalculateCalendar(int month_no, int week_no, int month_days){

    int i, s;
    //String[][] a = new String[6][7];
    for (i=0;i<week_no;i++)
        HoyahCalendar.a[i/7][i%7] = "";

    for(i=week_no; i<week_no + month_days; i++){
        s = i - week_no + 1;
        HoyahCalendar.a[i/7][i%7] = String.valueOf(s);
    }
    for(i=week_no+month_days; i<42; i++)


        HoyahCalendar.a[i/7][i%7] = "";
}
  }



 ////////// below is complete source code////////////////////


                      public class HoyahCalendar extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 findViews();
 setListeners();

 getDate();
 new ShowCalendar(mYear, mMonth);
 showOnScreen();
 }

 private static int mYear;
 private static int mMonth;
 private static int mDay;
 public static String[][] a = new String[6][7];
 private TextView date_today;
 private ImageButton last_month;
 private ImageButton next_month;
 private TextView e00;private TextView e01;private TextView e02;
 private TextView e03;private TextView e04;private TextView e05;
 private TextView e06;private TextView e10;private TextView e11;
 private TextView e12;private TextView e13;private TextView e14;
 private TextView e15;private TextView e16;private TextView e20;
 private TextView e21;private TextView e22;private TextView e23;
 private TextView e24;private TextView e25;private TextView e26;
 private TextView e30;private TextView e31;private TextView e32;
 private TextView e33;private TextView e34;private TextView e35;
 private TextView e36;private TextView e40;private TextView e41;
 private TextView e42;private TextView e43;private TextView e44;
 private TextView e45;private TextView e46;private TextView e50;
 private TextView e51;private TextView e52;private TextView e53;
 private TextView e54;private TextView e55;private TextView e56;

 protected static final int MENU_SELECT = Menu.FIRST;
 protected static final int MENU_BACK = Menu.FIRST+1;
 protected static final int MENU_QUIT = Menu.FIRST+3;
 protected static final int MENU_ABOUT = Menu.FIRST+2;



private void findViews(){
date_today = (TextView)findViewById(R.id.date_today);
last_month = (ImageButton)findViewById(R.id.last_month);
next_month = (ImageButton)findViewById(R.id.next_month);
e00 = (TextView)findViewById(R.id.e00);
e01 = (TextView)findViewById(R.id.e01);
e02 = (TextView)findViewById(R.id.e02);
e03 = (TextView)findViewById(R.id.e03);
e04 = (TextView)findViewById(R.id.e04);
e05 = (TextView)findViewById(R.id.e05);
e06 = (TextView)findViewById(R.id.e06);
e10 = (TextView)findViewById(R.id.e10);
e11 = (TextView)findViewById(R.id.e11);
e12 = (TextView)findViewById(R.id.e12);
e13 = (TextView)findViewById(R.id.e13);
e14 = (TextView)findViewById(R.id.e14);
e15 = (TextView)findViewById(R.id.e15);
e16 = (TextView)findViewById(R.id.e16);
e20 = (TextView)findViewById(R.id.e20);
e21 = (TextView)findViewById(R.id.e21);
e22  = (TextView)findViewById(R.id.e22);
e23 = (TextView)findViewById(R.id.e23);
e24 = (TextView)findViewById(R.id.e24);
e25  = (TextView)findViewById(R.id.e25);
e26 = (TextView)findViewById(R.id.e26);
e30 = (TextView)findViewById(R.id.e30);
e31 = (TextView)findViewById(R.id.e31);
e32  = (TextView)findViewById(R.id.e32);
e33 = (TextView)findViewById(R.id.e33);
e34 = (TextView)findViewById(R.id.e34);
e35 = (TextView)findViewById(R.id.e35);
e36 = (TextView)findViewById(R.id.e36);
e40 = (TextView)findViewById(R.id.e40);
e41 = (TextView)findViewById(R.id.e41);
e42 = (TextView)findViewById(R.id.e42);
e43 = (TextView)findViewById(R.id.e43);
e44 = (TextView)findViewById(R.id.e44);
e45 = (TextView)findViewById(R.id.e45);
e46 = (TextView)findViewById(R.id.e46);
e50 = (TextView)findViewById(R.id.e50);
e51 = (TextView)findViewById(R.id.e51);
e52  = (TextView)findViewById(R.id.e52);
e53 = (TextView)findViewById(R.id.e53);
e54 = (TextView)findViewById(R.id.e54);
e55 = (TextView)findViewById(R.id.e55);
e56 = (TextView)findViewById(R.id.e56);
} 

private void setListeners(){
last_month.setOnClickListener(lastMonth);
next_month.setOnClickListener(nextMonth);
}
//��һ��
private Button.OnClickListener lastMonth = new Button.OnClickListener() {
public void onClick(View v){
    if(mMonth == 1){
        mYear -= 1;
        mMonth = 12;
        new ShowCalendar(mYear, mMonth);
        showOnScreen();
    }else{
        mMonth -= 1;
        new ShowCalendar(mYear, mMonth);
        showOnScreen();
    }
}
};
 //��һ��
 private Button.OnClickListener nextMonth = new Button.OnClickListener() {
 public void onClick(View v){
    if(mMonth == 12){
        mYear += 1;
        mMonth = 1;
        new ShowCalendar(mYear, mMonth);
        showOnScreen();
    }else{
        mMonth += 1;
        new ShowCalendar(mYear, mMonth);
        showOnScreen();
    }
}
 };

 //��ȡ��ǰ����
public void getDate(){


Calendar mCalendar = Calendar.getInstance();
mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH)+1;
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);

 }

public void showOnScreen(){
date_today.setText(mYear + "Years" + mMonth + "Month");
 e00.setText(a[0][0]);e01.setText(""+a[0][1]); e02.setText(""+a[0]  [2]);
 e03.setText(""+a[0][3]); e04.setText(""+a[0][4]);e05.setText(""+a[0] [5]);
 e06.setText(""+a[0][6]); e10.setText(""+a[1][0]);e11.setText(""+a[1][1]);
 e12.setText(""+a[1][2]); e13.setText(""+a[1][3]);e14.setText(""+a[1][4]);
 e15.setText(""+a[1][5]); e16.setText(""+a[1][6]);e20.setText(""+a[2][0]);
 e21.setText(""+a[2][1]); e22.setText(""+a[2][2]);e23.setText(""+a[2][3]);
 e24.setText(""+a[2][4]); e25.setText(""+a[2][5]);e26.setText(""+a[2][6]);
 e30.setText(""+a[3][0]); e31.setText(""+a[3][1]);e32.setText(""+a[3][2]);
 e33.setText(""+a[3][3]); e34.setText(""+a[3][4]);e35.setText(""+a[3][5]);
 e36.setText(""+a[3][6]); e40.setText(""+a[4][0]);e41.setText(""+a[4][1]);
 e42.setText(""+a[4][2]); e43.setText(""+a[4][3]);e44.setText(""+a[4][4]);
 e45.setText(""+a[4][5]); e46.setText(""+a[4][6]);e50.setText(""+a[5][0]);
 e51.setText(""+a[5][1]); e52.setText(""+a[5][2]);e53.setText(""+a[5][3]);
 e54.setText(""+a[5][4]);  e55.setText(""+a[5][5]);e56.setText(""+a[5][6]);
 } 
 }







public class ShowCalendar {
private int mYear;
private int mMonth;
public ShowCalendar(int mYear, int mMonth){
this.mYear = mYear;
this.mMonth = mMonth;

calculateMonthFirstday();
} 

public void calculateMonthFirstday(){
int month, first_day=0;
if((mYear%4==0 && mYear%100!=0)||(mYear%400==0))
    month=1;
else
    month=0;

int y, y12, c, c12, m, d;
y = mYear%100;
y12 = (mYear-1)%100; //only for January and February
c = mYear/100;
c12 = (mYear-1)/100;
m = mMonth;
d = 1;

switch(mMonth){
case 1: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(13 + 1)/10 + d - 1;break;}
case 2: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(14 + 1)/10 + d - 1;break;}
case 4: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 5: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 6: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 7: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 8: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 9: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 10: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 11: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 12: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
}
if(first_day<0)
    first_day = 7 - (Math.abs(first_day))%7;//first_day每月第一天星期几
else
    first_day = first_day%7;

switch(mMonth){
    case 1: {CalculateCalendar(1,first_day,31);break;}
    case 2: {CalculateCalendar(2,first_day,28+month);break;}
    case 3: {CalculateCalendar(3,first_day,31);break;}
    case 4: {CalculateCalendar(4,first_day,30);break;}
    case 5: {CalculateCalendar(5,first_day,31);break;}
    case 6: {CalculateCalendar(6,first_day,30);break;}
    case 7: {CalculateCalendar(7,first_day,31);break;}
    case 8: {CalculateCalendar(8,first_day,31);break;}
    case 9: {CalculateCalendar(9,first_day,30);break;}
    case 10: {CalculateCalendar(10,first_day,31);break;}
    case 11: {CalculateCalendar(11,first_day,30);break;}
    case 12: {CalculateCalendar(12,first_day,31);break;}
 }
 }

 public void CalculateCalendar(int month_no, int week_no, int month_days){

 int i, s;
//String[][] a = new String[6][7];
for (i=0;i<week_no;i++)
    HoyahCalendar.a[i/7][i%7] = "";
for(i=week_no; i<week_no + month_days; i++){
    s = i - week_no + 1;
    HoyahCalendar.a[i/7][i%7] = String.valueOf(s);
}
for(i=week_no+month_days; i<42; i++)
    HoyahCalendar.a[i/7][i%7] = "";
}
}

0 个答案:

没有答案