黑莓开发学习入门系列,自己动手实现一个日历软件(六)

最近有点乱糟糟,还是继续写完剩下的:
根据  黑莓开发学习入门系列,自己动手实现一个日历软件(二)实现一个黑莓应用程序最基本的结构,我们真正的可以实现日历了:
新建一个工程 calender:作为日历的完整工程开始实现一个日历小程序 
创建类CalendarMain.java 作为日历的入口
创建日历布局外观类:CalenderLayout.java,作为主要的日历布局
CalendarMain类的应该如下结构:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.bulktree.calender;

import  net.rim.device.api.ui.UiApplication;
import  net.rim.device.api.ui.container.MainScreen;

public   class  CalendarMain  extends  UiApplication {

    
public   static   void  main(String[] args) {
        CalendarMain theApp 
=   new  CalendarMain();
        theApp.enterEventDispatcher();
    }

    
public  CalendarMain() {
        MainScreen mainScreen 
=   new  MainScreen();
        CalenderLayout calender 
=   new  CalenderLayout();
        mainScreen.add(calender.calLayout());
        pushScreen(mainScreen);
    }
}
黑莓程序入口总是先实现当前的屏幕 pushScreen(mainScreen)压入当前屏幕队列,然后维护主应用程序事件线程。
对于屏幕布局就很简单了:先画一个布局图 黑莓开发学习入门系列,自己动手实现一个日历软件(六) 
我用Excel模拟出一个日历,不同的颜色区域(当前日历红色除外),把整个日历作为分成三块,如果按照BlackBerry程序的方式,分为三个布局管理器,最上面是一个HorizontalFieldManager,日历的周那个区域也是HorizontalFieldManager,日期区域每行看成一个HorizontalFieldManager,整体看成一个VerticalFieldManager,而整个日历可看成一个VerticalFieldManager去规范里面的布局。这样一个完整的日历就出来了。具体实现如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.bulktree.calender;

import  java.util.Calendar;

import  net.rim.device.api.ui.Field;
import  net.rim.device.api.ui.FocusChangeListener;
import  net.rim.device.api.ui.Font;
import  net.rim.device.api.ui.Manager;
import  net.rim.device.api.ui.component.LabelField;
import  net.rim.device.api.ui.container.HorizontalFieldManager;
import  net.rim.device.api.ui.container.VerticalFieldManager;

public   class  CalenderLayout {
    
    
private   static  String[] wks  =  { " " " " " " " " " " " " " " };
    
    
private  LabelField ax  =   null ;
    
    
public  Manager calLayout() {
        Manager calender 
=   new  VerticalFieldManager();
        
// 日历头
        Manager calHeader  =   new  HorizontalFieldManager();
        Calendar cale 
=  Calendar.getInstance();
        ChineseCalendar chin 
=   new  ChineseCalendar(cale);
        String calHeaderString 
=   " 阴历: "   +  CalendarUtil.getCurrentYear()  +   " 年  "   +  chin.getChineseCalendarMonth()  +   " 月  " +  chin.getChinaDayString()  +   " 日{ "   +  chin.getCyclical()  +   " - "   +  chin.getAnimalsYear()  +   " 年} " ;
        ax 
=   new  LabelField(calHeaderString, LabelField.HCENTER);
        setFont(ax, 
13 1 );
        calHeader.add(ax);
        LabelField ay 
=   new  LabelField( "   阳历: "   +  CalendarUtil.getCurrentMonth()  +   " " );
        setFont(ay, 
13 1 );
        calHeader.add(ay);
        
        calender.add(calHeader);
        
// 星期头
        Manager weakHeader  =   new  HorizontalFieldManager();
        CalenderField whf 
=   null ;
        
        
for  ( int  i  =   0 ; i  <   7 ; i ++ ) {
            whf 
=   new  CalenderField(wks[i], CalenderField.RECTANGLE, CalenderField.NON_FOCUSABLE,  1 );
            setFont(whf, 
15 1 );
            weakHeader.add(whf);
        }
        calender.add(weakHeader);
        
        
// 日历体
         int  a  =  CalendarUtil.getWeekCurrntMonthFirstDay();
//         int a = CalendarUtil.getWeekMonthFirstDay(2011, 1);
         int  b  =  CalendarUtil.getCurrentDaysOfMonth();
//         int b = CalendarUtil.getDaysOfMonth(2011, 1);
        Manager calBody  =   new  HorizontalFieldManager();
        calBody.setBorder(
1 1 1 1 );
        
for  ( int  i  =   0 ; i  <  a; i ++ ) {
            calBody.add(
new  CalenderField( "" , CalenderField.RECTANGLE, CalenderField.NON_FOCUSABLE,  1 ));
        }
        CalenderField yac 
=   null ;
        CalenderField yc 
=   null ;
        
for  ( int  i  =   1 ; i  <=  b; i ++ ) {
            Manager cellManger 
=   new  VerticalFieldManager();
            yac 
=   new  CalenderField(i  +   "" , CalenderField.HCENTER, CalenderField.FOCUSABLE,  1 );
            setFont(yac, 
15 0 );
            cellManger.add(yac);
            Calendar cx 
=  Calendar.getInstance();
            cx.set(Calendar.DATE, i);
            
final  ChineseCalendar chines  =   new  ChineseCalendar(cx);
            yc 
=   new  CalenderField(chines.getChinaDayString(), CalenderField.HCENTER, CalenderField.NON_FOCUSABLE,  1 );
            setFont(yc, 
10 0 );
            
            yac.setFocusListener(
new  FocusChangeListener() {
                
public   void  focusChanged(Field field,  int  eventType) {
                    String text 
=   " 阴历: "   +  chines.getChineseCalenderYear()  +   " 年  "   +  chines.getChineseCalendarMonth()  +   " 月  " +  chines.getChinaDayString()  +   " 日{ "   +  chines.getCyclical()  +   " - "   +  chines.getAnimalsYear()  +   " 年} " ;
                    updateCalendarHeader(text);
                }
            });
            
            
if  (Calendar.getInstance().get(Calendar.DATE)  ==  i) {
                setFont(yac, 
15 1 );
                setFont(yc, 
10 1 );
            }
            cellManger.add(yc);
            calBody.add(cellManger);
            
if  ((i  +  a)  %   7   == 0 ) {
                calender.add(calBody); 
                calBody 
=   new  HorizontalFieldManager();
            }
        }
        calender.add(calBody);
        
        
return  calender;
    }

    
/**
     * 设置字体风格
     * 
@param  obj
     * 
@param  size
     * 
@param  isBlod 是否加粗, 1加粗, 0 正常
     
*/
    
private   void  setFont(Field obj,  int  size,  int  isBlod) {
        
int  fontStyle  =  isBlod  ==   1   ?  Font.BOLD : Font.PLAIN;
        obj.setFont(Font.getDefault().derive(fontStyle, size));
    }
    
    
private   void  updateCalendarHeader(String text) {
        ax.setText(text);
    }
}
这里面用到CalendarUtil工具类,ChineseCalendar农历工具类,CalenderField自定义的UI(上篇专门说明过)。现贴出代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.bulktree.calender;

import  java.util.Calendar;

/**
 * 日历工具类
 * 
 * 
@author  Administrator
 * 
 
*/
public   class  CalendarUtil {

    
private   static  Calendar cal  =  Calendar.getInstance();

    
public   static  String getCurrentYear() {
        
return   ""   +  cal.get(Calendar.YEAR);
    }

    
public   static  String getCurrentMonth() {
        
return   ""   +  (cal.get(Calendar.MONTH)  +   1 );
    }

    
// 返回当前月第一天的星期
     public   static   int  getWeekCurrntMonthFirstDay() {
        cal 
=  Calendar.getInstance();
        
return  getWeekMonthFirstDay(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
    }
    
    
// 返回指定月第一天的星期
     public   static   int  getWeekMonthFirstDay( int  year,  int  month) {
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, 
1 );
        
return  cal.get(Calendar.DAY_OF_WEEK)  -   1 ;
    }
    
    
// 返回当月天数
     public   static   int  getCurrentDaysOfMonth() {
        cal 
=  Calendar.getInstance();
        
return  getDaysOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
    }
    
// 返回指定月的天数
     public   static   int  getDaysOfMonth( int  year,  int  month) {
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month 
+   1 );
        cal.set(Calendar.DAY_OF_MONTH, 
1 );
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) 
- 1 );
        
return  cal.get(Calendar.DAY_OF_MONTH);
    }
}
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.bulktree.calender;

import  java.util.Calendar;

public   class  ChineseCalendar {   
    
private   int  year;   
    
private   int  month;   
    
private   int  day;   
    
private   boolean  leap;   
    
final   static  String chineseNumber[]  =  { " " " " " " " " " " " " " " " " " " " " " 十一 " " 十二 " };   
    
final   static   long [] lunarInfo  =   new   long []   
    {
0x04bd8 0x04ae0 0x0a570 0x054d5 0x0d260 0x0d950 0x16554 0x056a0 0x09ad0 0x055d2 ,   
     
0x04ae0 0x0a5b6 0x0a4d0 0x0d250 0x1d255 0x0b540 0x0d6a0 0x0ada2 0x095b0 0x14977 ,   
     
0x04970 0x0a4b0 0x0b4b5 0x06a50 0x06d40 0x1ab54 0x02b60 0x09570 0x052f2 0x04970 ,   
     
0x06566 0x0d4a0 0x0ea50 0x06e95 0x05ad0 0x02b60 0x186e3 0x092e0 0x1c8d7 0x0c950 ,   
     
0x0d4a0 0x1d8a6 0x0b550 0x056a0 0x1a5b4 0x025d0 0x092d0 0x0d2b2 0x0a950 0x0b557 ,   
     
0x06ca0 0x0b550 0x15355 0x04da0 0x0a5d0 0x14573 0x052d0 0x0a9a8 0x0e950 0x06aa0 ,   
     
0x0aea6 0x0ab50 0x04b60 0x0aae4 0x0a570 0x05260 0x0f263 0x0d950 0x05b57 0x056a0 ,   
     
0x096d0 0x04dd5 0x04ad0 0x0a4d0 0x0d4d4 0x0d250 0x0d558 0x0b540 0x0b5a0 0x195a6 ,   
     
0x095b0 0x049b0 0x0a974 0x0a4b0 0x0b27a 0x06a50 0x06d40 0x0af46 0x0ab60 0x09570 ,   
     
0x04af5 0x04970 0x064b0 0x074a3 0x0ea50 0x06b58 0x055c0 0x0ab60 0x096d5 0x092e0 ,   
     
0x0c960 0x0d954 0x0d4a0 0x0da50 0x07552 0x056a0 0x0abb7 0x025d0 0x092d0 0x0cab5 ,   
     
0x0a950 0x0b4a0 0x0baa4 0x0ad50 0x055d9 0x04ba0 0x0a5b0 0x15176 0x052b0 0x0a930 ,   
     
0x07954 0x06aa0 0x0ad50 0x05b52 0x04b60 0x0a6e6 0x0a4e0 0x0d260 0x0ea65 0x0d530 ,   
     
0x05aa0 0x076a3 0x096d0 0x04bd7 0x04ad0 0x0a4d0 0x1d0b6 0x0d250 0x0d520 0x0dd45 ,   
     
0x0b5a0 0x056d0 0x055b2 0x049b0 0x0a577 0x0a4b0 0x0aa50 0x1b255 0x06d20 0x0ada0 };   
  
    
// ====== 传回农历 y年的总天数   
     final   private   static   int  yearDays( int  y) {   
        
int  i, sum  =   348 ;   
        
for  (i  =   0x8000 ; i  >   0x8 ; i  >>=   1 ) {   
            
if  ((lunarInfo[y  -   1900 &  i)  !=   0 ) sum  +=   1 ;   
        }   
        
return  (sum  +  leapDays(y));   
    }   
  
    
// ====== 传回农历 y年闰月的天数   
     final   private   static   int  leapDays( int  y) {   
        
if  (leapMonth(y)  !=   0 ) {   
            
if  ((lunarInfo[y  -   1900 &   0x10000 !=   0 )   
                
return   30 ;   
            
else   
                
return   29 ;   
        } 
else   
            
return   0 ;   
    }   
  
    
// ====== 传回农历 y年闰哪个月 1-12 ,&nb#

你可能感兴趣的:(UI,.net,Excel,BlackBerry,黑莓)