2

大家好,我是黑莓新手,我想要一个可以滚动文本的文本字段,即水平大于首选宽度,还能够在文本绘制区域(例如左侧)之外显示标签。请帮我。

4

1 回答 1

4

这可以通过组合非滚动和滚动的 Horizo​​ntalFieldManager 来实现。

试试这个代码:

class Scr extends MainScreen {
    public Scr() {
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        editHolder.add(new TextField(TextField.NO_NEWLINE));
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}

设置默认文本代码:

class Scr extends MainScreen {
    public Scr() {
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField(TextField.NO_NEWLINE);      
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);

        // set some text then
        String text = "Lorem ipsum dolor sit amet, consectetuer"+
        " adipiscing elit, sed diam nonummy nibh euismod tincidunt"+
        " ut laoreet dolore magna aliquam erat volutpat.";
        textField.setText(text);
    }
}

还有一些基本上适用于 4.6/4.7 的东西:

class Scr extends MainScreen {
    public Scr() {
        String text = "Lorem ipsum dolor sit amet, consectetuer"+
        " adipiscing elit, sed diam nonummy nibh euismod tincidunt"+
        " ut laoreet dolore magna aliquam erat volutpat.";
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField("",text,1024,TextField.NO_NEWLINE);
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}

经理边框

Border border = BorderFactory.createSimpleBorder(new XYEdges(4,4,4,4));
fieldHolder.setBorder(border);

固定尺寸管理器

class SizedHFM extends HorizontalFieldManager {
    int mWidth = 0;

    public SizedHFM(int width) {
        super(NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        mWidth = width;
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(mWidth, maxHeight);
        setExtent(mWidth, getPreferredHeight());
    }
}

使用示例:

class Scr extends MainScreen {
    public Scr() {
        String text = "Lorem ipsum dolor sit amet, consectetuer"
                + " adipiscing elit, sed diam nonummy nibh euismod tincidunt"
                + " ut laoreet dolore magna aliquam erat volutpat.";
        SizedHFM fieldHolder = new SizedHFM(200);
        Border border = BorderFactory
                .createSimpleBorder(new XYEdges(4, 4, 4, 4));
        fieldHolder.setBorder(border);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField("", text, 1024,
                TextField.NO_NEWLINE);
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}
于 2010-03-30T07:23:39.193 回答