BlackBerry 客户定制组件(二)OneDayField(日期组件)
效果图:
前提准备:
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;
}
}
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) {
}
}
}
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) {
}
}
}
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) {
}
}
}