0

我尝试做的是简单地更改 EditField 的高度。这听起来很容易,但我面临一个让我发疯的问题。这是我的代码:

public class CMainManager extends HorizontalFieldManager
{
    EditField mtxt=new EditField(EditField.FOCUSABLE | EditField.FILTER_DEFAULT);

    /* Constructeur */
    public CMainManager()
    {
        add(mtxt);
    }

     public int getPreferredHeight()
     {
          return Display.getHeight();
    }

    public int getPreferredWidth()
    {
          return Display.getWidth();
    }

    protected void sublayout(int width, int height)
    {
        layoutChild(mtxt, 50, 500);
        setPositionChild(mtxt, 0, 10);
        mtxt.setBackground(
                BackgroundFactory.createSolidBackground(0x000000FF));

        setExtent(width, height);
    }
}

行 layoutChild(mtxt, 50, 500); 应该设置我的 EditField 的宽度和高度。确实如此,但仅限于宽度。高度是恒定的。如果我将其设置为 500,我将得到与将其设置为 10 相同的结果。

知道我缺少什么吗?因为我确定这是我正在做的一个愚蠢的错误......

谢谢

==编辑

在 jproffit 帮助之后,我明白了我做错了什么。但是现在它可以工作了(我可以设置高度)我还有另一个问题。这是我的代码:

private EditField m_Txt=new EditField(EditField.FOCUSABLE |
                                        EditField.FILTER_DEFAULT) {
    protected void layout(int width, int height)
    {
        setExtent(Display.getWidth(), m_TxtHeight);
    }
    public boolean isFocusable()
    {
        return true;
    }

    protected void onFocus(int direction)
    {
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }
};

使用此代码,我无法正确管理焦点。我忘记了什么?我在我的字段中看不到光标。

任何想法 ?

谢谢

4

2 回答 2

2

您不是在告诉它要占用的尺寸,而是在指定它可以占用的最大空间量。因此,当您告诉它 10、50 或 500 时,如果它只想使用 8 的高度,这就是全部。layout()如果您还想控制其尺寸,则必须覆盖EditField。

于 2011-04-27T18:27:59.013 回答
0

尝试这样做..创建一个自定义编辑字段并在布局方法中

_editField = new EditField("", initialValueOfTextField) 
    {
        protected void layout(int maxWidth, int maxHeight) 
        {
          int myWidth = getMaxSize() * getFont().getAdvance("W");// define your req width
          int myHeight= 50 // your required height
          super.layout(myWidth, myHeight);
       }
   };
于 2011-12-01T10:53:43.543 回答