如何实现正确的水平滚动自定义编辑字段



因此,我们正在尝试实现3个文本编辑字段,供人们在我们的黑莓应用程序中输入他们的用户凭据。然而,当用户名的长度超过编辑字段框时,我们会遇到麻烦(字段必须在框内,框必须保持静止,文本必须水平滚动。)

这是我的自定义可滚动编辑字段类,基于这里的可滚动编辑域:

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的方式。

现在,出于某种原因,当我运行此代码时,字段就在那里,我可以键入它,但它就好像字段有一个字符宽,我看不到它右边的任何东西,也看不到字段或边界。有人能帮我发现我做错了什么吗?

试试这个代码(我已经删除了一些多余的代码)

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()是冗余的。

相关内容

  • 没有找到相关文章

最新更新