Blackberry - 如何使DateField的datepicker弹出窗口居中

时间:2013-03-06 16:50:13

标签: blackberry datepicker

我的屏幕中央有DateField

enter image description here

当我触摸它时,我看到:

enter image description here

但我希望看到:

enter image description here

我的问题是:

如何将DateField的日期选择器弹出窗口居中?

Blackberry OS 6

更新

public class MyMainScreen extends MainScreen
{
    protected DateField dateField_ = null;
    protected VerticalFieldManager container_ = null;

    public MyMainScreen()
    {
        super();
        container_ = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL
            | VerticalFieldManager.VERTICAL_SCROLLBAR | VerticalFieldManager.USE_ALL_HEIGHT)
        {
            protected void sublayout(int width, int height)
            {
                super.sublayout(width, height);

                layoutChild(dateField_ , 100, 40);
                setPositionChild(dateField_ , (width - 100) / 2, 50);

                setVirtualExtent(width, 50);
            }
        };

        add(container_);
        dateField_ = new DateField("", new Date(), new SimpleDateFormat("HH:mm"), Field.FIELD_HCENTER |
                DrawStyle.HCENTER | Field.FOCUSABLE);

        container_.add(dateField_);
        add(container_);
    }
}

2 个答案:

答案 0 :(得分:1)

好的,所以有一些问题:

  1. DateField不会仅仅因为您将Field.FIELD_HCENTER | DrawStyle.HCENTER传递给构造函数而使字段居中。我认为DateField甚至不会识别这些旗帜。

  2. VerticalFieldManager是一种特殊的Manager,旨在按照您add()的顺序从上到下垂直布置其子项。如果您要以这种方式布置字段,那么我不知道您应该扩展VerticalFieldManager。如果您想实施Manager,只需直接延长sublayout()即可。此外,在您的实施中调用super.sublayout()可能会导致问题。

  3. 您正在硬编码DateField的高度/宽度。我不确定它是否允许。

  4. 您的代码实际上并没有为我编译,因为DateField构造函数的日期为long,而不是Date。这与居中无关。

  5. 所以,我建议这样的事情:

      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
    
      dateField_ = new DateField("", (new Date()).getTime(), new SimpleDateFormat("HH:mm"), Field.FOCUSABLE);
    
      container_ = new Manager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) {
         public int getPreferredHeight() {
            return Display.getHeight();
         }
         public int getPreferredWidth() {
            return Display.getWidth();
         }
         protected void sublayout(int width, int height) {            
            int h = Math.min(height, Display.getHeight());
            setExtent(width, h);
            // only needed if there's actually more content than fits in the visible area
            //setVirtualExtent(width, h + ?);
    
            int dfWidth = dateField_.getPreferredWidth();
            int dfHeight = dateField_.getPreferredHeight();
            layoutChild(dateField_, dfWidth, dfHeight);
            setPositionChild(dateField_, (width - dfWidth) / 2, (h - dfHeight) / 2);
         }                                      
      };
    
      container_.add(dateField_);      
      add(container_);
    

    P.S。我不确定你是否想要滚动。只有DateField,没有理由滚动。但是,我知道您的真实Screen可能包含更多内容,因此您可能需要setVirtualExtent(),我已将其注释掉。

答案 1 :(得分:0)

此解决方案也适用于我:

package com.my.package;

import java.util.Calendar;
import java.util.Date;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.DateTimePicker;

public class MyDateManager extends VerticalFieldManager {
    protected DateField dateField_ = null;
    protected DateTimePicker dateTimePicker_ = null;
    protected SimpleDateFormat dateFormat_ = null;

    public MyDateManager() {
        super(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
        dateFormat_ = new SimpleDateFormat("HH:mm");
        dateField_ = new DateField("", new Date().getTime(), dateFormat_,
                Field.FIELD_HCENTER | DrawStyle.HCENTER | Field.FOCUSABLE) {
            protected boolean touchEvent(TouchEvent paramTouchEvent) {
                if (paramTouchEvent.getEvent() == TouchEvent.CLICK) {
                    dateField_.setEnabled(false);
                    showDatePicker();
                }
                return super.touchEvent(paramTouchEvent);
            }

            protected boolean trackwheelClick(int paramInt1, int paramInt2) {
                dateField_.setEnabled(false);
                showDatePicker();

                return super.trackwheelClick(paramInt1, paramInt2);
            }
        };
        this.add(dateField_);
    }

    protected void sublayout(int width, int height) {
        layoutChild(dateField_, width, height);
        setPositionChild(dateField_, (width - dateField_.getWidth()) / 2,
                (height - dateField_.getHeight()) / 2);
        int h = dateField_.getHeight();
        if (h <= 0)
            h = height;
        setExtent(width, h);
    }

    protected void showDatePicker() {
        if (dateTimePicker_ == null) {
            Calendar cal = Calendar.getInstance();

            // HACK
            String dateFormat = dateFormat_.toPattern().indexOf("H", 0) == -1 ? dateFormat_
                    .toPattern() : null;
            String timeFormat = dateFormat_.toPattern().indexOf("H", 0) != -1 ? dateFormat_
                    .toPattern() : null;

            dateTimePicker_ = DateTimePicker.createInstance(cal, dateFormat,
                    timeFormat);
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    if (dateTimePicker_.doModal()) {
                        dateField_.setDate(dateTimePicker_.getDateTime()
                                .getTime());
                    }

                    dateField_.setEnabled(true);
                    dateField_.setFocus();
                    dateTimePicker_ = null;
                }
            });
        }
    }
}