Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface BlackBerry-创建自定义日期字段_User Interface_Datetime_Blackberry_Custom Controls_Datetimepicker - Fatal编程技术网

User interface BlackBerry-创建自定义日期字段

User interface BlackBerry-创建自定义日期字段,user-interface,datetime,blackberry,custom-controls,datetimepicker,User Interface,Datetime,Blackberry,Custom Controls,Datetimepicker,我想创建一个看起来像这样的字段…有人能告诉我如何做到这一点吗?我可以使用哪个字段?在哪里,我可以使用轨迹球/轮子选择值,或者在暴风雨中,我可以滑动 在5.0 API中引入了一个新字段,名为。对于您的具体情况,还有一个“仅限日期/时间”选项。对于5.0之前的操作系统,您需要创建自己的自定义控件。它可以是简单的标签或位图字段扩展,带有导航事件处理 您需要保留字符串数组,其值将在水平导航时在圆圈中更改。 如果这样的自定义字段是聚焦的,则绘制上下数组 所以,有一个这样的字段用于星期几,一个用于月,一个

我想创建一个看起来像这样的字段…有人能告诉我如何做到这一点吗?我可以使用哪个字段?在哪里,我可以使用轨迹球/轮子选择值,或者在暴风雨中,我可以滑动


在5.0 API中引入了一个新字段,名为。对于您的具体情况,还有一个“仅限日期/时间”选项。对于5.0之前的操作系统,您需要创建自己的自定义控件。

它可以是简单的标签或位图字段扩展,带有导航事件处理

您需要保留字符串数组,其值将在水平导航时在圆圈中更改。
如果这样的自定义字段是聚焦的,则绘制上下数组

所以,有一个这样的字段用于星期几,一个用于月,一个用于月日,一个用于年

将它们全部放在HorizontalFieldManager中,该管理器将放在弹出窗口的Upscreen中,在弹出窗口上关闭所有值并合成日期值(可通过FieldChangeListener传递)

更新

风暴支援 在带控制盘的设备上,导航单击可轻松实现保存和关闭功能,但在暴风雨中,这将是一个问题(每个字段焦点操作将关闭对话框)。要解决此问题,请添加“确定”和“取消”按钮

此外,还添加了touchEvent,以使用触摸单击处理适当的值更改

您可以为RIM OS=4.7保留不同的实现,并在构建任务中替换它们

来源

类DatePickerDialog扩展PopupScreen实现FieldChangeListener{
DatePickerField mDatePicker;
纽扣领域mOKButton;
按钮字段McCancelButton;
公共日期选择器对话框(日期){
super(新的VerticalFieldManager(),弹出屏幕。默认设置为关闭);
添加(mDatePicker=newdatepickerfield(date));
//评论RIM OS<4.7
添加按钮();
}
私有void addButtons(){
HorizontalFieldManager hfm=新的HorizontalFieldManager(字段输入);
添加(hfm);
mOKButton=新建按钮字段(“确定”,按钮字段。单击);
mOKButton.setChangeListener(此);
hfm.add(mOKButton);
McCancelButton=新建按钮字段(“取消”,按钮字段。单击);
setChangeListener(这个);
hfm.add(mCancelButton);
}
公共无效设置日期(日期值){
mDatePicker.setDate(日期值);
}
公共日期getDate(){
返回mDatePicker.getDate();
}
公共日期选择器Dialog(){
这个(Calendar.getInstance().getTime());
}
//评论RIM OS<4.7
公共无效字段已更改(字段,int上下文){
if(mOKButton==字段){
getChangeListener().fieldChanged(此为0);
close();
}else if(mCancelButton==字段){
close();
}
}
//评论RIM OS>4.6
//受保护的布尔导航单击(int状态,int时间){
//getChangeListener().fieldChanged(此为0);
//close();
//返回super.navigationClick(状态、时间);
// }
类DatePickerField扩展HorizontalFieldManager实现
FieldChangeListener{
私有字符串[]daysOfWeek=新字符串[]{“星期日”、“星期一”,
“星期二”、“星期三”、“星期四”、“星期五”,
“星期六”};
私有字符串[]个月=新字符串[]{“一月”、“二月”、“三月”、“四月”,
“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”,
“Dec”};
私有整数mDayOfMonth=10;
私有int-mMonth=1;
私有int-mYear=2009;
私有StrTimeField mDayOfWeekField;
私有StrTimeField和monthfield;
私有NumTimeField mDayOfMonthField;
私人NumTimeField mYearField;
日历=Calendar.getInstance();
公共日期选择器字段(日期){
日历。设置时间(日期);
mYear=calendar.get(calendar.YEAR);
//Calendar.JANUARY==0,so+1值
MONTH=calendar.get(calendar.MONTH);
mDayOfMonth=calendar.get(calendar.DAY\u OF\u MONTH);
int dayOfWeek=calendar.get(calendar.DAY/u周);
//我认为最好禁用星期几编辑
mDayOfWeekField=新的标准字段(每周的天数,每周的天数-1,
非聚焦);
mDayOfWeekField.setChangeListener(此);
添加(mDayOfWeekField);
AmmonthField=新的标准字段(月,mm);
AmmonthField.setChangeListener(此);
添加(字段);
mDayOfMonthField=新的NumTimeField(mDayOfMonth,1,31);
mDayOfMonthField.setChangeListener(此);
添加(mDayOfMonthField);
mYearField=新的NumTimeField(mYear,1900,2012);
setChangeListener(this);
添加(mYearField);
}
公共无效字段已更改(字段,int上下文){
mDayOfMonth=mDayOfMonthField.getValue();
calendar.set(calendar.DAY\u OF_MONTH,mDayOfMonth);
month=monthfield.getValue();
日历.set(日历.月,月);
mYear=mYearField.getValue();
calendar.set(calendar.YEAR,mYear);
int dayOfWeek=calendar.get(calendar.DAY/u-OF-u-WEEK)-1;
mDayOfWeekField.setIndex(dayOfWeek);
}
公共日期getDate(){
返回calendar.getTime();
}
公共作废设置日期(日期){
日历。设置时间(日期);
mYear=calendar.get(calendar.YEAR);
MONTH=calendar.get(calendar.MONTH);
mDayOfMonth=calendar.get(calendar.DAY\u OF\u MONTH);
int dayOfWeek=calendar.get(calendar.DAY/u周);
设置索引(dayOfWeek-1);
ammonthfield.setIndex(ammonth);
设置值(mDayOfMonth);
mYearField.setValue(mYear);
}
class DatePickerDialog extends PopupScreen implements FieldChangeListener {

    DatePickerField mDatePicker;
    ButtonField mOKButton;
    ButtonField mCancelButton;

    public DatePickerDialog(Date date) {
        super(new VerticalFieldManager(), PopupScreen.DEFAULT_CLOSE);
        add(mDatePicker = new DatePickerField(date));

        // comment on RIM OS < 4.7
        addButtons();
    }

    private void addButtons() {
        HorizontalFieldManager hfm = new HorizontalFieldManager(FIELD_HCENTER);
        add(hfm);
        mOKButton = new ButtonField("OK", ButtonField.CONSUME_CLICK);
        mOKButton.setChangeListener(this);
        hfm.add(mOKButton);
        mCancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
        mCancelButton.setChangeListener(this);
        hfm.add(mCancelButton);
    }

    public void setDate(Date dateValue) {
        mDatePicker.setDate(dateValue);
    }

    public Date getDate() {
        return mDatePicker.getDate();
    }

    public DatePickerDialog() {
        this(Calendar.getInstance().getTime());
    }

    // comment on RIM OS < 4.7
    public void fieldChanged(Field field, int context) {
        if (mOKButton == field) {
            getChangeListener().fieldChanged(this, 0);
            close();
        } else if (mCancelButton == field) {
            close();
        }
    }

    // comment on RIM OS > 4.6
    // protected boolean navigationClick(int status, int time) {
    // getChangeListener().fieldChanged(this, 0);
    // close();
    // return super.navigationClick(status, time);
    // }

    class DatePickerField extends HorizontalFieldManager implements
            FieldChangeListener {
        private String[] daysOfWeek = new String[] { "Sunday", "Monday",
                "Tuesday", "Wednesday", "Thursday", "Friday", 
                "Saturday" };
        private String[] months = new String[] { "Jan", "Feb", "Mar", "Apr",
                "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
                "Dec" };
        private int mDayOfMonth = 10;
        private int mMonth = 1;
        private int mYear = 2009;

        private StrTimeField mDayOfWeekField;
        private StrTimeField mMonthField;
        private NumTimeField mDayOfMonthField;
        private NumTimeField mYearField;
        Calendar calendar = Calendar.getInstance();

        public DatePickerField(Date date) {
            calendar.setTime(date);
            mYear = calendar.get(Calendar.YEAR);
            // Calendar.JANUARY == 0, so +1 value
            mMonth = calendar.get(Calendar.MONTH);
            mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            // think it's better to disable Day Of Week edit
            mDayOfWeekField = new StrTimeField(daysOfWeek, dayOfWeek - 1,
                    NON_FOCUSABLE);
            mDayOfWeekField.setChangeListener(this);
            add(mDayOfWeekField);
            mMonthField = new StrTimeField(months, mMonth);
            mMonthField.setChangeListener(this);
            add(mMonthField);
            mDayOfMonthField = new NumTimeField(mDayOfMonth, 1, 31);
            mDayOfMonthField.setChangeListener(this);
            add(mDayOfMonthField);
            mYearField = new NumTimeField(mYear, 1900, 2012);
            mYearField.setChangeListener(this);
            add(mYearField);
        }

        public void fieldChanged(Field field, int context) {
            mDayOfMonth = mDayOfMonthField.getValue();
            calendar.set(calendar.DAY_OF_MONTH, mDayOfMonth);
            mMonth = mMonthField.getValue();
            calendar.set(calendar.MONTH, mMonth);
            mYear = mYearField.getValue();
            calendar.set(calendar.YEAR, mYear);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            mDayOfWeekField.setIndex(dayOfWeek);
        }

        public Date getDate() {
            return calendar.getTime();
        }

        public void setDate(Date date) {
            calendar.setTime(date);
            mYear = calendar.get(Calendar.YEAR);
            mMonth = calendar.get(Calendar.MONTH);
            mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            mDayOfWeekField.setIndex(dayOfWeek - 1);
            mMonthField.setIndex(mMonth);
            mDayOfMonthField.setValue(mDayOfMonth);
            mYearField.setValue(mYear);
        }

        abstract class TimeField extends LabelField {
            int mWidth = 0;

            public TimeField() {
                super("", FOCUSABLE);
            }

            public TimeField(long style) {
                super("", style);
            }

            protected abstract void switchValue(int step);

            protected boolean navigationMovement(int dx, int dy, 
                    int status, int time) {
                if (Math.abs(dy) > Math.abs(dx)) {
                    switchValue(-dy);
                return true;
                } else
                    return super.navigationMovement(dx, dy, status, time);
            }

            boolean prepared = false;

            protected void onFocus(int direction) {
                prepared = false;
                super.onFocus(direction);
            }

            protected void onUnfocus() {
                invalidate();
                super.onUnfocus();
            }

            // comment on RIM OS < 4.7
            protected boolean touchEvent(TouchEvent msg) {
                if (isFocus() && msg.getEvent() == TouchEvent.CLICK) {
                    if (!prepared) {
                        prepared = true;
                    } else {
                        int y = msg.getY(1);
                        int cy = getTop() + (getHeight() >> 1);
                        switchValue((y > cy) ? -1 : 1);
                    }
                }
                return false;
            }

            public int getPreferredWidth() {
                return mWidth;
            }

            public int getPreferredHeight() {
                return super.getPreferredHeight() + 24;
            }

            protected void layout(int width, int height) {
                super.layout(width, height);
                setExtent(getPreferredWidth(), getPreferredHeight());
            }

            protected void paint(Graphics graphics) {
                String text = getText();
                Font font = getFont();
                int x = (getPreferredWidth() 
                    - font.getAdvance(text)) >> 1;
                int y = (getPreferredHeight() - font.getHeight()) >> 1;
                graphics.drawText(text, x, y);
                if (isFocus()) {
                    graphics.setColor(Color.WHITE);
                    int xc = (getPreferredWidth() >> 1);
                    int y1 = 10, y2 = 0, x2 = xc - 9, x1 = xc + 9;

                    int[] xPts = new int[] { x1, x2, xc };
                    int[] yPts = new int[] { y1, y1, y2 };
                    graphics.drawFilledPath(xPts, yPts, 
                        null, null);

                    y2 = getPreferredHeight();
                    y1 = y2 - 10;

                    yPts = new int[] { y1, y1, y2 };
                    graphics.drawFilledPath(xPts, yPts, 
                        null, null);
                }
            }

            public abstract int getValue();
        }

        class StrTimeField extends TimeField {
            String[] mValues;
            int mIndex;

            public StrTimeField(String[] values) {
                this(values, 0);
            }

            public StrTimeField(String[] values, int value) {
                this(values, value, FOCUSABLE);
            }

            public StrTimeField(String[] values, int value, long style) {
                super(style);
                mValues = values;
                setIndex(value);
                Font font = getFont();
                for (int i = 0; i < mValues.length; i++) {
                    int width = font.getAdvance(mValues[i]);
                    mWidth = Math.max(mWidth, width);
                }
                mWidth += 10;
            }

            protected void switchValue(int step) {
                int index = mIndex + step;
                if (index < 0 || index >= mValues.length)
                    index += ((index > 0) ? -1 : 1) 
                        * mValues.length;
                setIndex(index);
            }

            private void setIndex(int index) {
                if (index >= 0 && index < mValues.length) {
                    mIndex = index;
                    setText(mValues[mIndex]);
                }
            }

            public int getValue() {
                return mIndex;
            }

        }

        class NumTimeField extends TimeField {
            int mValue;
            int mMinValue;
            int mMaxValue;

            public NumTimeField(int val, int minVal, int maxVal) {
                this(val, minVal, maxVal, FOCUSABLE);
            }

            public NumTimeField(int val, int minVal, int maxVal,
                    long style) {
                super(style);
                mValue = val;
                mMinValue = minVal;
                mMaxValue = maxVal;

                setText(String.valueOf(mValue));
                int maxDig = String.valueOf(mMaxValue).length();
                String test = "";
                for (int i = 0; i < maxDig; i++)
                    test += "0";
                mWidth = getFont().getAdvance(test);
                mWidth += 10;
            }

            protected void switchValue(int step) {
                int value = mValue + step;
                if (value > mMaxValue)
                    value = value - (mMaxValue - mMinValue + 1);
                if (value < mMinValue)
                    value = value + (mMaxValue - mMinValue + 1);
                setValue(value);
            }

            private void setValue(int value) {
                mValue = value;
                setText(String.valueOf(mValue));
            }

            public int getValue() {
                return mValue;
            }
        }
    }
}