黑莓 - 自定义大小EditField中 [英] Blackberry - Custom size EditField

查看:225
本文介绍了黑莓 - 自定义大小EditField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个对话框看起来应该像这样的:

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

在以下领域
填写
___________ 喜欢 ________________

其中_行是EditFields。

where the "_" lines are the EditFields.

我坚持在Horizo​​ntalFieldManager所有字段,这是我到对话框。不幸的是,第一EditField中消耗所有的第一行的空间。我曾尝试通过创建扩展BasicEditField我自己的类重写EditField中的get preferredWidth()方法,但都没有成功。

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?

推荐答案

就像DaveJohnston说:

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

这篇关于黑莓 - 自定义大小EditField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆