如何为自定义Choicefield创建弹出窗口并调用它?

时间:2012-01-04 05:59:35

标签: blackberry custom-controls

我通过扩展ChoiceField创建了我自己的简单ObjectChoiceField ..就像......

public class MyChoiceField extends ObjectChoiceField 
{

    public MyChoiceField(String label ,Object[] choices) 
    {
        super(label, choices);
    }

    protected void layout(int width, int height) 
    {
        setMinimalWidth(width/2-62);
        super.layout(width, height);
    }

    public void paint(Graphics graphics)
    {
        super.paint(graphics);
    }    
}

现在我想在自定义菜单中选择MyChoiceField时显示我的选择(不是黑莓在用户点击ChoiceField时提供的默认弹出窗口)..

我怎样才能实现它?示例代码将是可观的.. 感谢

1 个答案:

答案 0 :(得分:0)

尝试覆盖 fieldChangeNotify(int context) ObjectChoiceField 方法,如下所示:

public class MyChoiceField extends ObjectChoiceField 
{

    public MyChoiceField(String label ,Object[] choices) 
    {
        super(label, choices);
    }

    protected void layout(int width, int height) 
    {
        setMinimalWidth(width/2-62);
        super.layout(width, height);
    }

    protected void fieldChangeNotify(int context) {

        int index = getSelectedIndex();

        try{
            VerticalFieldManager vfm = new VerticalFieldManager();

            vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));

            PopupScreen popUpScreen = new PopupScreen(vfm){
                public boolean onClose()
                {
                    this.close();
                    return true;
                }
            };

            UiApplication.getUiApplication().pushScreen(popUpScreen);

        } catch(Exception exc) {
            System.out.println("Exception in choiceField fieldChangeNotify");
        }
    }

    public void paint(Graphics graphics)
    {
        super.paint(graphics);
    }
}

现在使用 MyChoiceField ,就像这样:

String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);            
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));

让我知道这个解决方案是否有效。