【问题标题】:Focus in ListField?专注于 ListField?
【发布时间】:2012-03-29 23:16:55
【问题描述】:

我在屏幕上添加了两个列表字段 但在焦点发生变化时,它不会水平转到第二个列表。

class TestScreen extends MainScreen {
         private final ObjectListField listField = new ObjectListField(FIELD_LEFT)
         {
             public void layout(int width, int height)
              {
                super.layout(width,height);
                setExtent(Display.getWidth()/2, Display.getHeight());
              }

         };
         private final ObjectListField listField2 = new ObjectListField(FIELD_RIGHT)
         {
             public void layout(int width, int height)
              {
                super.layout(width,height);
                setExtent(Display.getWidth()/2, Display.getHeight());
              }

         };
         private final String[] lines = { "Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6" };
         private final String[] lines2 = { "Line 10", "Line 20", "Line 30", "Line 40", "Line 50", "Line 60" };
         TestScreen() 
         {
                  super(NO_VERTICAL_SCROLL);
                  HorizontalFieldManager hfm=new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL);
                  hfm.add(listField);
                  hfm.add(listField2);
                  listField.set(lines);
                  listField2.set(lines2);
                  add(hfm);

        }
}

我想在焦点 frim 列表 1 到列表 2 上水平移动。

【问题讨论】:

  • 你能提供一个简短的代码sn-p吗?

标签: blackberry


【解决方案1】:

在两个横向管理器中组织 4 个字段时,我们遇到了类似的问题。从 upper 右侧字段向下滚动将我们带到 upper 左侧而不是 bottom 右侧,因为移动首先在水平管理器内滚动.

我们最终实现了自己的管理器来添加和定位字段(而不是水平管理器),然后覆盖管理器的 navigationMovement 函数,如 this tutorial 中所述,以控制哪个字段获得焦点,具体取决于移动。例如:

if (dx < 0)
{
    getField(focusedIndex - 1).setFocus();
}

基本上dx 是正/负取决于水平线的移动,dy 是正/负取决于垂直线的移动。

希望这对您有所帮助,如果您找到更好的方法,我很乐意了解它,因为这很麻烦......

编辑 - 代码:我添加了一些代码示例,但我们只有 4 个按钮,因此导航与您要查找的不同。我们在屏幕中创建经理并将每个字段添加到其中,然后将经理添加到屏幕。我们在 Manager 中覆盖了 2 个方法,sublayout 和 navigationMovement。

sublayout 函数负责布局按钮,注意您必须明确设置 x 和 y 位置,在确定位置时最好使用 Display.getWidth 以便在不同屏幕上保持相同的比例.

protected void sublayout(int width, int height)
{
    int count = getFieldCount();
    Field field;
    for (int i = 0; i < count; i++)
    {
        field = getField(i);
        layoutChild(field, field.getWidth(), field.getHeight());
        setPositionChild(field, xPositionOfButton, yPositionOfButton);
    }

    setExtent(width, height);
}

对于 navigationMovement,使用 dx 和 dy 的值以及当前具有焦点的字段的索引来确定下一个要获得焦点的字段。如果不应该改变焦点,只需在不改变焦点的情况下返回 false,例如,当从最左边的字段向左移动时,您可能希望留在同一个字段上而不是环绕。

public boolean navigationMovement(int dx, int dy, int status, int time)
{
    int focusedIndex = getFieldWithFocusIndex();

    if (dx < 0)
    {
        getField(focusedIndex - 1).setFocus();
    }
    else if (dx > 0)
    {
        getField(focusedIndex + 1).setFocus();
    }
    else if (dy < 0)
    {
        getField(focusedIndex - 2).setFocus();
    }
    else if (dy > 0)
    {
        getField(focusedIndex + 2).setFocus();
    }
    else
        return false;

    return true;
}

我希望这会有所帮助:)

【讨论】:

  • @amit 我在答案正文中添加了示例代码和 cmets。
【解决方案2】:

还有另一种解决方案,您可以使用任何管理器结构:

protected boolean navigationMovement(int dx, int dy,
            int status, int time) {

        Field focusedIndex = ThisScreen.getFieldWithFocus().getLeafFieldWithFocus();
        if (focusedIndex == YourFocusableField)
        { 
           //any needed settings here
           if (dx < 0)
           {
               anyField.setFocus();
           }
           else if (dx > 0)
           {
               anyField.setFocus();
           }
           else if (dy < 0)
           {
               anyField.setFocus();
           }
           else if (dy > 0)
           {
               anyField.setFocus();
           }
           else
              return false;

           return true;
        }
    }

但在这种情况下,您需要为每个可熔的字段都这样做

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多