如何实现适当的水平可滚动自定义EditField?

时间:2012-03-29 17:47:34

标签: java blackberry blackberry-editfield

因此,我们正在尝试实施3个文本编辑字段,供人们在我们的黑莓应用中输入用户凭据。但是,例如,当用户的名字比编辑字段框更长时,我们遇到了麻烦(字段必须在框内,并且框必须保持静止,文本必须是水平滚动的。)

以下是我的自定义可滚动编辑字段类based on the scrollable edit field here

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class ScrollableEditField extends Manager {
private final static int        DEFAULT_TOP_PADDING     = 1;
private final static int        DEFAULT_BOTTOM_PADDING  = 1;
private final static int        DEFAULT_LEFT_PADDING    = 1;
private final static int        DEFAULT_RIGHT_PADDING   = 1; 

private int                     TOTAL_VERTICAL_PADDING  = DEFAULT_TOP_PADDING + DEFAULT_BOTTOM_PADDING;
private int                     TOTAL_HORIZONTAL_PADDDING = DEFAULT_LEFT_PADDING + DEFAULT_RIGHT_PADDING;

private int                     width  = -1;
private int                     height = -1;

private HorizontalFieldManager  hfm = new HorizontalFieldManager(HORIZONTAL_SCROLL);
private EditField               ef;

public ScrollableEditField(String label, String initialValue, int maxNumChars, long innerEditFieldStyle) {
    super(NO_HORIZONTAL_SCROLL);
    ef = new EditField(label, initialValue, maxNumChars, innerEditFieldStyle);
    hfm.add(ef);
    add(hfm);
}

protected void sublayout(int width, int height) {
    if (this.width != -1) {
        width = this.width;
    }

    if (this.height != -1) {
        height = this.height;
    } else {
        height = ef.getFont().getHeight();
    }

    layoutChild(hfm, width-TOTAL_HORIZONTAL_PADDDING, height-TOTAL_VERTICAL_PADDING);
    setPositionChild(hfm, DEFAULT_LEFT_PADDING, DEFAULT_TOP_PADDING);
    setExtent(width, height); // Maybe not..
}    

public EditField getEditField() {
    return ef;
}

public void setWidth(int width) {
    this.width = width;
}
public void setHeight(int height) {
    this.height = height;
}
protected void onFocus(int direction) {
    super.onFocus(direction);
    ef.setCursorPosition(0);
}

protected void onUnfocus() {
    hfm.setHorizontalScroll(0);
    super.onUnfocus();
}
};

这就是我在代码中使用这个类的方法:

我将每个字段放入HorizontalFieldManager,然后将此hfm放在网格单元格中(具有固定大小)。以下是几行:

GridFieldManager gfm = new GridFieldManager(1, 2, 0);
gfm.setColumnProperty(0, GridFieldManager.FIXED_SIZE, Display.getWidth()* 1/5);
gfm.setColumnProperty(1, GridFieldManager.PREFERRED_SIZE_WITH_MAXIMUM,    Display.getWidth()* 4/5);

HorizontalFieldManager hfm1e = new HorizontalFieldManager(USE_ALL_WIDTH|USE_ALL_HEIGHT);

ScrollableEditField sef1 = new ScrollableEditField("", "", 32, EditField.FILTER_EMAIL);
sef1.setWidth((Display.getWidth()* 4/5)-5);
sef1.setHeight(getFont().getHeight());
sef1.setBorder(BorderFactory.createRoundedBorder(new XYEdges(1,1,1,1)));
hfm1e.add(sef1);
gfm2.add(hfm1e);

这些最后一行只是来自一个更大的类,但是它们说明了我尝试将自定义编辑字段添加到GUI的方式。

现在由于某种原因,当我运行此代码时,字段就在那里,我可以输入它,但是它好像字段是一个字符宽而且我看不到它右边的任何内容,也不能看到字段或者边界。任何人都可以帮助发现我做错了吗?

1 个答案:

答案 0 :(得分:0)

试试这段代码(我删除了一些冗余代码)

GridFieldManager gfm = new GridFieldManager(1, 2, 0);
gfm.setColumnProperty(0, GridFieldManager.FIXED_SIZE, Display.getWidth()* 1/5);
gfm.setColumnProperty(1, GridFieldManager.PREFERRED_SIZE_WITH_MAXIMUM, Display.getWidth()* 4/5);

ScrollableEditField sef1 = new ScrollableEditField("", "", 32, EditField.FILTER_EMAIL);
sef1.setBorder(BorderFactory.createRoundedBorder(new XYEdges(1,1,1,1)));
gfm2.add(sef1);

sef1的大小由其经理gfm确定。因此,hfm1esef1.setWidth()sef2.setWindth()是多余的。