黑莓 - 自定义尺寸 EditField

发布于 2024-08-03 05:58:39 字数 313 浏览 6 评论 0原文

我正在尝试组合一个应如下所示的对话框:

填写以下字段
_______________ 喜欢 ____________________,

其中“_”行是编辑字段。

我将所有字段粘贴到 Horizo​​ntalFieldManager 中,并将其添加到对话框中。不幸的是,第一个 EditField 占用了第一行的所有空间。我尝试通过创建自己的扩展 BasicEditField 的类来重写 EditField 的 getPreferredWidth() 方法,但没有成功。

当然,必须有一种简单的方法来强制编辑字段具有一定的大小。我缺少什么?

I am trying to put together a dialog that should look like this:

Fill in the below fields
_______________ likes ____________________

where the "_" lines are the EditFields.

I am sticking all the fields in a HorizontalFieldManager, which I add to the dialog. Unfortunately, the first EditField consumes all the space on the first line. I have tried to override the getPreferredWidth() method of the EditField by creating my own class extending BasicEditField, but have had no success.

Surely there must be a simple way to force a certain size for an edit field. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2024-08-10 05:58:39

正如戴夫约翰斯顿所说:

class LikesHFManager extends HorizontalFieldManager {
    EditField mEditFieldLeft;
    LabelField mLabelField;
    EditField mEditFieldRight;
    String STR_LIKES = "likes";
    int mLabelWidth = 0;
    int mEditWidth = 0;
    int mOffset = 4;

    public LikesHFManager() {
        mEditFieldLeft = new EditField();
        mLabelField = new LabelField(STR_LIKES);
        mEditFieldRight = new EditField();

        mLabelWidth = mLabelField.getFont().getAdvance(STR_LIKES);
        int screenWidth = Display.getWidth();
        mEditWidth = (screenWidth - mLabelWidth) >> 1;
        mEditWidth -= 2 * mOffset;

        // calculate max with of one character
        int chMaxWith = mEditFieldLeft.getFont().getAdvance("W");
        // calculate max count of characters in edit field
        int chMaxCnt = mEditWidth / chMaxWith;

        mEditFieldLeft.setMaxSize(chMaxCnt);
        mEditFieldRight.setMaxSize(chMaxCnt);

        add(mEditFieldLeft);
        add(mLabelField);
        add(mEditFieldRight);
    }

    protected void sublayout(int maxWidth, int maxHeight) {

        int x = 0;
        int y = 0;

        int editHeight = mEditFieldLeft.getPreferredHeight();
        int labelHeight = mLabelField.getPreferredHeight();

        setPositionChild(mEditFieldLeft, x, y);
        layoutChild(mEditFieldLeft, mEditWidth, editHeight);
        x += mEditWidth;
        x += mOffset;

        setPositionChild(mLabelField, x, y);
        layoutChild(mLabelField, mLabelWidth, labelHeight);
        x += mLabelWidth;
        x += mOffset;

        setPositionChild(mEditFieldRight, x, y);
        layoutChild(mEditFieldRight, mEditWidth, editHeight);
        x += mEditWidth;

        setExtent(x, Math.max(labelHeight, editHeight));
    }
}

Just like DaveJohnston said:

class LikesHFManager extends HorizontalFieldManager {
    EditField mEditFieldLeft;
    LabelField mLabelField;
    EditField mEditFieldRight;
    String STR_LIKES = "likes";
    int mLabelWidth = 0;
    int mEditWidth = 0;
    int mOffset = 4;

    public LikesHFManager() {
        mEditFieldLeft = new EditField();
        mLabelField = new LabelField(STR_LIKES);
        mEditFieldRight = new EditField();

        mLabelWidth = mLabelField.getFont().getAdvance(STR_LIKES);
        int screenWidth = Display.getWidth();
        mEditWidth = (screenWidth - mLabelWidth) >> 1;
        mEditWidth -= 2 * mOffset;

        // calculate max with of one character
        int chMaxWith = mEditFieldLeft.getFont().getAdvance("W");
        // calculate max count of characters in edit field
        int chMaxCnt = mEditWidth / chMaxWith;

        mEditFieldLeft.setMaxSize(chMaxCnt);
        mEditFieldRight.setMaxSize(chMaxCnt);

        add(mEditFieldLeft);
        add(mLabelField);
        add(mEditFieldRight);
    }

    protected void sublayout(int maxWidth, int maxHeight) {

        int x = 0;
        int y = 0;

        int editHeight = mEditFieldLeft.getPreferredHeight();
        int labelHeight = mLabelField.getPreferredHeight();

        setPositionChild(mEditFieldLeft, x, y);
        layoutChild(mEditFieldLeft, mEditWidth, editHeight);
        x += mEditWidth;
        x += mOffset;

        setPositionChild(mLabelField, x, y);
        layoutChild(mLabelField, mLabelWidth, labelHeight);
        x += mLabelWidth;
        x += mOffset;

        setPositionChild(mEditFieldRight, x, y);
        layoutChild(mEditFieldRight, mEditWidth, editHeight);
        x += mEditWidth;

        setExtent(x, Math.max(labelHeight, editHeight));
    }
}
随梦而飞# 2024-08-10 05:58:39

尝试子类化 Horizo​​ntalFieldManager 并重写 sublayout 方法:

protected void sublayout(int maxWidth, int maxHeight) { }

在此方法中,您应该为要添加的每个组件调用 setPositionChild() 和 layoutChild() ,以便可以控制每个组件的位置和大小。

您还应该重写每个组件的布局方法,并调用

setExtent(getPreferredWidth(), getPreferredHeight()); 

它,这将利用您已经编写的 getPreferred... 方法的实现。

希望这有帮助。

Try subclassing HorizontalFieldManager and override the sublayout method:

protected void sublayout(int maxWidth, int maxHeight) { }

In this method you should call setPositionChild() and layoutChild() for each component you are adding so you can control the positioning and size of each.

You should also override the layout method of each component and call

setExtent(getPreferredWidth(), getPreferredHeight()); 

this will make use of your implementation of the getPreferred... methods you have already written.

Hope this helps.

瑾夏年华 2024-08-10 05:58:39

基于 Max Gontar 的解决方案,这应该可以解决为 Horizo​​ntalFieldManagers 的子字段分配宽度的一般问题:

import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;

public class FieldRowManager extends HorizontalFieldManager {
    public FieldRowManager(final long style)
    {
        super(style);
    }
    public FieldRowManager()
    {
        this(0);
    }

    private SubField FirstSubField = null;
    private SubField LastSubField = null;
    private static class SubField
    {
        public final Field Field;
        public final int Width;
        public final int Offset;
        private SubField Next;
        public SubField(final FieldRowManager container, final Field field, final int width, final int offset)
        {
            Field = field;
            Width = width;
            Offset = offset;

            if (container.LastSubField == null)
            {
                container.FirstSubField = this;
            }
            else
            {
                container.LastSubField.Next = this;
            }
            container.LastSubField = this;
        }
        public SubField getNext()
        {
            return Next;
        }
    }

    public void add(final Field field)
    {
        add(field, field.getPreferredWidth());
    }
    public void add(final Field field, final int width)
    {
        add(field, width, 0);
    }
    public void add(final Field field, final int width, final int offset)
    {
        new SubField(this, field, width, offset);
        super.add(field);
    }

    protected void sublayout(final int maxWidth, final int maxHeight)
    {
        int x = 0;
        int height = 0;
        SubField subField = FirstSubField;
        while (subField != null)
        {
            final Field field = subField.Field;
            final int fieldHeight = field.getPreferredHeight();
            this.setPositionChild(field, x, 0);
            this.layoutChild(field, subField.Width, fieldHeight);
            x += subField.Width+subField.Offset;
            if (fieldHeight > height)
            {
                height = fieldHeight;
            }

            subField = subField.getNext();
        }
        this.setExtent(x, height);
    }
}

只需调用 add 方法的重载即可指定宽度以及下一个字段之前的偏移空间。尽管这不允许删除/替换字段。

令人烦恼的是,RIM 在标准库中没有提供此功能。 Horizo​​ntalFieldManager应该以这种方式工作。

Building on Max Gontar's solution, this should solve the general problem of assigning width to sub Fields of HorizontalFieldManagers:

import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;

public class FieldRowManager extends HorizontalFieldManager {
    public FieldRowManager(final long style)
    {
        super(style);
    }
    public FieldRowManager()
    {
        this(0);
    }

    private SubField FirstSubField = null;
    private SubField LastSubField = null;
    private static class SubField
    {
        public final Field Field;
        public final int Width;
        public final int Offset;
        private SubField Next;
        public SubField(final FieldRowManager container, final Field field, final int width, final int offset)
        {
            Field = field;
            Width = width;
            Offset = offset;

            if (container.LastSubField == null)
            {
                container.FirstSubField = this;
            }
            else
            {
                container.LastSubField.Next = this;
            }
            container.LastSubField = this;
        }
        public SubField getNext()
        {
            return Next;
        }
    }

    public void add(final Field field)
    {
        add(field, field.getPreferredWidth());
    }
    public void add(final Field field, final int width)
    {
        add(field, width, 0);
    }
    public void add(final Field field, final int width, final int offset)
    {
        new SubField(this, field, width, offset);
        super.add(field);
    }

    protected void sublayout(final int maxWidth, final int maxHeight)
    {
        int x = 0;
        int height = 0;
        SubField subField = FirstSubField;
        while (subField != null)
        {
            final Field field = subField.Field;
            final int fieldHeight = field.getPreferredHeight();
            this.setPositionChild(field, x, 0);
            this.layoutChild(field, subField.Width, fieldHeight);
            x += subField.Width+subField.Offset;
            if (fieldHeight > height)
            {
                height = fieldHeight;
            }

            subField = subField.getNext();
        }
        this.setExtent(x, height);
    }
}

Just call the overloads of the add method to specify the width, and the offset space before the next Field. Though this does not allow for deleting/replacing Fields.

It is irksome that RIM does not provide this functionality in the standard library. HorizontalFieldManager should just work this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文