隨筆 - 35  文章 - 21  trackbacks - 0
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜


          效果圖:



          前提準備:
          1,使用到一個上一次發布的組件:ImageLabelField。這個組件是一個圖標按鈕。
          可以接收用戶的點擊動作。
          2,一個關于時間格式的類 DateFormatOutil。

          行為:
          點擊左右的圖標按鈕,可以更換日期的顯示。
          并提供了獲得當前日期的函數:public Date getCurrentDate()

          代碼:
          DateFormatOutil
          package app.ui.outil;

          import java.util.Calendar;
          import java.util.Date;
          /**
           * 
           
          */
          public class DateFormatOutil {
           
              
          public final static long MILLISECONDS_IN_A_DAY = 86400000;
              
          public final static long MILLISECONDS_IN_AN_HOUR = 3600000;
              
          public final static long MILLISECONDS_IN_A_MINUTE = 60000;
              
              
          private static Calendar calendar = Calendar.getInstance();
              
              
          public static String formatDateAsFr_dMhm( java.util.Date date)
              {
                 
          //format : dd/MM hh:mm
                 calendar.setTime( date );
                 
                 
          int day = calendar.get(Calendar.DAY_OF_MONTH);
                 
          int month = calendar.get(Calendar.MONTH) + 1;
                 
          int hour = calendar.get(Calendar.HOUR_OF_DAY);
                 
          int minute = calendar.get(Calendar.MINUTE);
                
                 
          return formatXX(day)+ "/" +formatXX(month)
                        
          + " " +formatXX(hour)+ ":" +formatXX(minute);
              }
              
              
          public static String formatDateAsFr_dMy( java.util.Date date)
              {
                 
          //format : dd/MM/yyyy
                 calendar.setTime( date );
                 
                 
          int day = calendar.get(Calendar.DAY_OF_MONTH);
                 
          int month = calendar.get(Calendar.MONTH) + 1;
                 
          int year = calendar.get(Calendar.YEAR);
                 
          return formatXX(day)+ "/" +formatXX(month)+"/"+year;
              }
              
              
          public static String formatDateAsFr_dMyhm( Date date )
              {
                 
          //format : dd/MM/yyyy hh:mm
                 calendar.setTime( date );
                 
                 
          int day = calendar.get(Calendar.DAY_OF_MONTH);
                 
          int month = calendar.get(Calendar.MONTH) + 1;
                 
          int year = calendar.get(Calendar.YEAR);
                 
          int hour = calendar.get(Calendar.HOUR_OF_DAY);
                 
          int minute = calendar.get(Calendar.MINUTE);
                 
                 
          return formatXX(day)+ "/" +formatXX(month) + "/" +year
                        
          + " " +formatXX(hour)+ ":" +formatXX(minute);
                        
              }
              
              
          public static Date addDays( Date date, int num )
              {
                  
          long mseconds = date.getTime();
                  
          return new Date( mseconds + MILLISECONDS_IN_A_DAY*num );
              }
                  
              
          public static Date getBeginOfDay( Date date )
              {
                 
          long mseconde = date.getTime();
                 
          long plusmseconde = mseconde % MILLISECONDS_IN_A_DAY;
                 Date beginningDate 
          = new Date( mseconde - plusmseconde - MILLISECONDS_IN_AN_HOUR );
                 
          return beginningDate;
              }
              
              
          public static Date getEndOfDay( Date date )
              {
                 
          long mseconds = date.getTime();
                 
          long plusmseconds = mseconds % MILLISECONDS_IN_A_DAY;
                 
          long restmseconds = MILLISECONDS_IN_A_DAY - plusmseconds ;
                 Date endDate 
          = new Date( mseconds + restmseconds - MILLISECONDS_IN_AN_HOUR -1);
                 
          return endDate;
              }
              
              
          private static String formatXX( int value )
              {
                  
          return value < 10 ? "0"+value : ""+value;
              }
              
              
          }


          OneDayField :
          package app.ui.component;

          import app.ui.outil.DateFormatOutil;

          import java.util.Date;
          import net.rim.device.api.ui.Field;
          import net.rim.device.api.ui.Keypad;
          import net.rim.device.api.ui.container.HorizontalFieldManager;
          import net.rim.device.api.ui.component.LabelField;
          import net.rim.device.api.system.Bitmap;

          /**
           * 
           
          */
          public class OneDayField extends HorizontalFieldManager
          {    
              
          private Date currentDate;
              
          private LabelField dateChamp;
              
          private ImageLabelField previousButton;
              
          private ImageLabelField nextButton;
              
              
          public OneDayField( Date date, long style) 
              {   
                  
          super( style );
                  currentDate 
          = date;
                  
                  String dateStr 
          = DateFormatOutil.formatDateAsFr_dMy( currentDate );
                  dateChamp 
          = new LabelField( dateStr, Field.NON_FOCUSABLE );
                  
                  Bitmap previousImage 
          = Bitmap.getBitmapResource( "arrow_date_previous.gif" );
                  previousButton 
          = new ImageLabelField( previousImage );
                  
                  Bitmap nextImage 
          = Bitmap.getBitmapResource( "arrow_date_next.gif" );
                  nextButton 
          = new ImageLabelField( nextImage );
                  
                  add( previousButton );
                  add( dateChamp );
                  add( nextButton );
              }


              
          public int getPreferredWidth()
              {
                  
          int width = 0;
                  
          for (int i = 0;  i < getFieldCount();  i++) {
                      width 
          += getField(i).getPreferredWidth();
                  }
                  
          return width;
              }
              
              
          public int getPreferredHeight()
              {
                  
          int height = 0;
                  
          for (int i = 0;  i < getFieldCount();  i++) {
                      
          int currentHeight = getField(i).getPreferredHeight();
                      height 
          = height > currentHeight ? height : currentHeight;
                  }
                  
          return height;
              }
              
              
          public Date getCurrentDate()
              {
                  
          return currentDate;
              }
              
              
          public void setCurrentDate( Date newDate )
              {
                  currentDate 
          = newDate;
                  dateChamp.setText( DateFormatOutil.formatDateAsFr_dMy( currentDate ) );
              }
              
              
              
          protected boolean keyDown(int keycode, int time)
              {
                  
          if( Keypad.key( keycode ) == Keypad.KEY_ENTER)
                  { 
                     Field field 
          = getFieldWithFocus();
                     
          if( field == previousButton )
                     {
                         setCurrentDate( DateFormatOutil.addDays( currentDate, 
          -1 ) );
                     }     
                     
          else if( field == nextButton )
                     {
                         setCurrentDate( DateFormatOutil.addDays( currentDate, 
          1 ) );
                     }  
                
                  }  
                  
          else
                  {
                    fieldChangeNotify(
          1);
                    
          return false;
                  }
                  fieldChangeNotify(
          1);
                  
          return true;
              }
               
              
          protected void fieldChangeNotify(int context) 
              {
                  
          try {
                      
          this.getChangeListener().fieldChanged(this, context);
                  } 
          catch (Exception exception) {
                  }
              }

          }


          SevenDayField :
          package app.ui.component;

          import app.ui.outil.DateFormatOutil;

          import java.util.Date;
          import net.rim.device.api.ui.Field;
          import net.rim.device.api.ui.Keypad;
          import net.rim.device.api.ui.container.HorizontalFieldManager;
          import net.rim.device.api.ui.component.LabelField;
          import net.rim.device.api.system.Bitmap;

          /**
           * 
           
          */
          public class SevenDayField extends HorizontalFieldManager
          {
              
          private Date beginDate;
              
          private Date middleDate;
              
          private Date endDate;
              
          private LabelField beginDateChamp;
              
          private LabelField endDateChamp;
              
          private ImageLabelField previousButton;
              
          private ImageLabelField nextButton;
              
              
          public SevenDayField( Date date, long style) 
              {
                  
                  
          super( style );
                  
                  middleDate 
          = date;
                  
                  beginDate 
          = DateFormatOutil.addDays( middleDate, -3 );
                  String beginDateStr 
          = DateFormatOutil.formatDateAsFr_dMy( beginDate );
                  beginDateChamp 
          = new LabelField( beginDateStr, Field.NON_FOCUSABLE );
                  
                  endDate 
          = DateFormatOutil.addDays( middleDate, 3 );
                  String endDateStr 
          = DateFormatOutil.formatDateAsFr_dMy( endDate );
                  endDateChamp 
          = new LabelField( endDateStr, Field.NON_FOCUSABLE );
                  
                  Bitmap previousImage 
          = Bitmap.getBitmapResource( "arrow_date_previous.gif" );
                  previousButton 
          = new ImageLabelField( previousImage );
                  
                  Bitmap nextImage 
          = Bitmap.getBitmapResource( "arrow_date_next.gif" );
                  nextButton 
          = new ImageLabelField( nextImage );
                  
                  add( previousButton );
                  add( beginDateChamp );
                  add( 
          new LabelField( "-" ) );
                  add( endDateChamp );
                  add( nextButton );  
                        
              }
              
              
          public int getPreferredWidth()
              {
                  
          int width = 0;
                  
          for (int i = 0;  i < getFieldCount();  i++) {
                      width 
          += getField(i).getPreferredWidth();
                  }
                  
          return width;
              }
              
              
          public int getPreferredHeight()
              {
                  
          int height = 0;
                  
          for (int i = 0;  i < getFieldCount();  i++) {
                      
          int currentHeight = getField(i).getPreferredHeight();
                      height 
          = height > currentHeight ? height : currentHeight;
                  }
                  
          return height;
              }    
              
              
          public Date getBeginDate()
              {
                  
          return beginDate;
              }
              
              
          public Date getMiddleDate()
              {
                  
          return middleDate;
              }
              
              
          public Date getEndDate()
              {
                  
          return endDate;
              }
              
              
          public void setCurrentDate( Date newMiddleDate )
              {
                  middleDate 
          = newMiddleDate;
                  beginDate 
          = DateFormatOutil.addDays( middleDate, -3 );
                  endDate 
          = DateFormatOutil.addDays( middleDate, 3);
                  beginDateChamp.setText( DateFormatOutil.formatDateAsFr_dMy( beginDate ) ); 
                  endDateChamp.setText( DateFormatOutil.formatDateAsFr_dMy( endDate ) );
              }

                      
              
          protected boolean keyDown(int keycode, int time)
              {
                  
          if( Keypad.key( keycode ) == Keypad.KEY_ENTER)
                  { 
                     Field field 
          = getFieldWithFocus();
                     
                     
          if( field == previousButton )
                     {
                         setCurrentDate( DateFormatOutil.addDays( middleDate, 
          -1 ) );
                     }     
                     
          else if( field == nextButton )
                     {
                         setCurrentDate( DateFormatOutil.addDays( middleDate, 
          1 ) );
                     } 
                  }  
                  
          else
                  {
                    fieldChangeNotify(
          1);
                    
          return false;
                  }
                  fieldChangeNotify(
          1);
                  
          return true;
              }
               
              
          protected void fieldChangeNotify(int context) 
              {
                  
          try {
                      
          this.getChangeListener().fieldChanged(this, context);
                  } 
          catch (Exception exception) {
                  }
              }
                
          }


          posted on 2008-10-20 02:24 lincode 閱讀(316) 評論(0)  編輯  收藏 所屬分類: Blackberry
          主站蜘蛛池模板: 南丹县| 中卫市| 清水河县| 耿马| 海淀区| 桓台县| 拜泉县| 三亚市| 都江堰市| 江北区| 乐陵市| 九江市| 喀喇沁旗| 栾城县| 察隅县| 六安市| 延庆县| 肇庆市| 凌海市| 麻江县| 滨州市| 沾益县| 化隆| 芦溪县| 民县| 大英县| 阿拉尔市| 进贤县| 壤塘县| 呼图壁县| 朔州市| 东平县| 郁南县| 叶城县| 剑河县| 资源县| 响水县| 延寿县| 沭阳县| 乌海市| 玛沁县|