1

嘿,在我的屏幕上有一个编辑字段和 2 个自定义按钮字段,分别为“确定”和“取消”按钮字段下方有一些更可聚焦的标签字段

当我在编辑字段中写一个名字并按下回车键时,焦点来到“确定”按钮,但如何将焦点设置在“取消​​”按钮上。

此外,在滚动焦点时不会自动向前移动???

我该怎么做可能会与触摸事件及其处理混淆!!!

请帮忙!!!!!!!!!!!!

代码:

        txt_Name = new EditField(TextField.NO_NEWLINE)
            {
                public void paint(net.rim.device.api.ui.Graphics g)
                {
                    g.setColor(Color.MAROON);
                    super.paint(g);
                }
           };

            txt_Name.setFont(font);

     v1 =  new VerticalFieldManager();
     v1.add(txt_Name );


    ButtonField btn1 = new ButtonField("OK",ButtonField.CONSUME_CLICK);
    ButtonField btn2 = new ButtonField("CANCEL",ButtonField.CONSUME_CLICK);

     v2 =  new VerticalFieldManager();
     v2.add(btn1);
     v2.add(btn2);

    LabelField l1 = new  LabelField("Hello Moon ",Field.Focussable);
    LabelField l2 = new  LabelField("Hello Citizen",Field.Focussable);
    LabelField l3 = new  LabelField("Hello People",Field.Focussable);
    LabelField l4 = new  LabelField("Hello world",Field.Focussable);

     v3 =  new VerticalFieldManager();
     v3.add(l1);
     v3.add(l2);
     v3.add(l3);
     v3.add(l4);

    add(v1);
    add(v2);
    add(v3);
}


protected boolean navigationClick(int status, int time) 
    {
     if(OK.isFocus())
             {
                //execute some code
                return true;
             }
if(CANCEL.isFocus())
             {
                //execute some code
                return true;
             }
        }
4

3 回答 3

1

我就像马克建议的那样,为每个按钮单独的 FieldChangeListeners:

class Scr extends MainScreen {
    EditField txt_Name;
    ButtonField btnOK;
    ButtonField btnCancel;
    VerticalFieldManager v1;
    VerticalFieldManager v2;
    VerticalFieldManager v3;
    Font font = Font.getDefault().derive(Font.BOLD, 20);

    public Scr() {
        txt_Name = new EditField(TextField.NO_NEWLINE) {
            public void paint(net.rim.device.api.ui.Graphics g) {
                g.setColor(Color.MAROON);
                super.paint(g);
            }
        };

        txt_Name.setFont(font);

        v1 = new VerticalFieldManager();
        v1.add(txt_Name);

        btnOK = new ButtonField("OK", ButtonField.CONSUME_CLICK);
        btnOK.setChangeListener(
            new FieldChangeListener(){
                public void fieldChanged(Field field, int context) {
            Dialog.inform("OK pressed");
        }});
        btnCancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
        btnCancel.setChangeListener(
            new FieldChangeListener(){
                public void fieldChanged(Field field, int context) {
            Dialog.inform("Cancel pressed");
        }});
        v2 = new VerticalFieldManager();
        v2.add(btnOK);
        v2.add(btnCancel);

        LabelField l1 = new LabelField("Hello Moon", Field.FOCUSABLE);
        LabelField l2 = new LabelField("Hello Citizen", Field.FOCUSABLE);
        LabelField l3 = new LabelField("Hello People", Field.FOCUSABLE);
        LabelField l4 = new LabelField("Hello world", Field.FOCUSABLE);

        v3 = new VerticalFieldManager();
        v3.add(l1);
        v3.add(l2);
        v3.add(l3);
        v3.add(l4);

        add(v1);
        add(v2);
        add(v3);
    }
}

现在似乎没问题:
alt text http://img40.imageshack.us/img40/6472/textentered.jpg alt text http://img59.imageshack.us/img59/7574/okpressed.jpg alt text http:// /img641.imageshack.us/img641/9246/cancelpressed01.jpg

更新

Swati 我可以使用 Storm 模拟器单击鼠标单击这些按钮。除了模拟器中的 cod 文件来自旧版本之外,我找不到任何其他解释。您可以通过更改代码中的任何标签文本来快速检查它,然后部署并检查此更改是否将应用于设备上的应用程序。如果没有,这是旧版本,您应该清理模拟器并再次部署应用程序。
希望对你有帮助!
另请参阅BlackBerry - 更改未反映在我的应用程序中

于 2010-03-08T08:39:23.777 回答
1
    txt_Name = new EditField(TextField.NO_NEWLINE)
            {
                public void paint(net.rim.device.api.ui.Graphics g)
                {
                    g.setColor(Color.MAROON);
                    super.paint(g);
                }
           };

            txt_Name.setFont(font);

     v1 =  new VerticalFieldManager();
     v1.add(txt_Name );


    ButtonField btn1 = new ButtonField("OK",ButtonField.CONSUME_CLICK);
    ButtonField btn2 = new ButtonField("CANCEL",ButtonField.CONSUME_CLICK);

     h2 =  new HorizontalalFieldManager();
     h2.add(btn1);
     h2.add(btn2);

    LabelField l1 = new  LabelField("Hello Moon ",Field.Focussable);
    LabelField l2 = new  LabelField("Hello Citizen",Field.Focussable);
    LabelField l3 = new  LabelField("Hello People",Field.Focussable);
    LabelField l4 = new  LabelField("Hello world",Field.Focussable);

     v3 =  new VerticalFieldManager();
     v3.add(l1);
     v3.add(l2);
     v3.add(l3);
     v3.add(l4);

    add(v1);
    add(h2);
    add(v3);
}


protected boolean navigationClick(int status, int time) 
    {
int index = h2.getFieldwithFocusIndex();
     if(h2==0)
             {
                //execute some code for OK
                return true;
             }
if(h2==1)
             {
                //execute some code for cancel
                return true;
             }
        }
于 2010-03-12T10:00:40.380 回答
0

您可以通过覆盖屏幕或包含可聚焦字段(例如确定和取消按钮)的管理器上的“navigationMovement”方法来控制焦点顺序。只需评估传递给该方法的参数,使用 Field.setFocus() 将焦点设置在所需字段上,然后返回 true。

至于焦点不会随着滚动而移动——这正是触摸屏 UI 的工作方式。焦点仅在您触摸可聚焦字段时移动 - 因此,如果您在“轻弹”屏幕滚动时未触摸任何可聚焦字段,则焦点不会改变。

于 2010-03-07T06:39:42.320 回答