BlackBerry - 滚动固定大小的文本编辑,左侧和边框带有静态标签

时间:2010-03-30 07:01:11

标签: user-interface blackberry scroll border edit

大家好我不熟悉黑莓,我想要一个文本字段滚动它的文本,即水平地大于首选宽度,也能够在文本绘制区域外显示标签(例如在左侧)。请帮我。

1 个答案:

答案 0 :(得分:4)

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

试试这段代码:

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);
    }
}