如何在标签上设置焦点,直到在黑莓中没有选择另一个标签

时间:2012-02-06 11:58:00

标签: blackberry

我正在使用PaneManagerDemo项目代码(该项目存在于BB的示例演示项目中)用于开发选项卡。 当我选择tab得到焦点时,我遇到一个问题,但当我选择该选项卡下方的数据时,焦点转到该数据。 我想当用户在选项卡下选择数据时,它必须关注该数据以及选项卡,以便用户可以在哪个选项卡下了解所选数据。 我没有得到它。 请告诉我。 这是参考的代码。

public class PaneManagerDemo extends UiApplication
{ 

public static void main(String[] args)
{
    UiApplication app = new PaneManagerDemo();
    app.enterEventDispatcher();
}


public PaneManagerDemo()
{      
    invokeLater(new Runnable()
    {
        public void run()
        {
            int headerType = 0;

            // Display a dialog for user to select header type
            OptionDialog dialog = new OptionDialog();
            int result = dialog.doModal();
            if(result == Dialog.OK)
            {
                headerType = dialog.getHeaderType();
            }
            else if(result == Dialog.CANCEL)
            {
                System.exit(0);
            }   

            //PaneScreen screen = new PaneScreen(headerType);
            PaneScreen screen = new PaneScreen(headerType);
            pushScreen(screen);
        }
    });        
}


/**
 * A dialog popup used to choose a header type
 */
private static class OptionDialog extends Dialog
{        
    public static final int SCROLL_HEADER_TYPE = 0;
    public static final int TAB_HEADER_TYPE = 1;

    private ObjectChoiceField _choiceField;     

    /**
     * Create a new HeaderDialog object
     */
    public OptionDialog()
    {
        super(Dialog.D_OK_CANCEL, "Choose Header Type", Dialog.OK, null, Dialog.GLOBAL_STATUS);
        _choiceField = new ObjectChoiceField("", new String[]{"Scrollable", "Tab"}, 0);
        add(_choiceField);            
        _choiceField.setFocus();
    }


    /**
     * Returns an integer representing the header type
     * 
     * @return SCROLL_HEADER_TYPE if scrollable header selected, TAB_HEADER_TYPE if tab header selected
     */
    public int getHeaderType()
    {
        return _choiceField.getSelectedIndex();
    }
}


/**
 * Main screen for the application. Displays three panes
 * switchable via horizontal scroll field or tabs, depending
 * on user selection.
 */
private final static class PaneScreen extends MainScreen
{   
    /**
     * Creates a new PaneScreen object
     * @param headerType The header type for the PaneManager, scrollable or tab style 
     */
    public PaneScreen(int headerType)        
    {
        super(Field.FOCUSABLE);                       

        // Instantiate the model for the pane manager and enable looping
        PaneManagerModel model = new PaneManagerModel();
        model.enableLooping(true);

        // Create a pane
        VerticalFieldManager vfm = new VerticalFieldManager();
        vfm.add(new LabelField("Data 1"));            
        XYEdges edgesOne = new XYEdges(1, 1, 1, 1);
        vfm.setBorder(BorderFactory.createRoundedBorder(edgesOne));                
        Pane pane = new Pane(new LabelField("Pane 1", Field.FOCUSABLE | Field.FIELD_HCENTER), vfm);

        // Add the pane to the model
        model.addPane(pane);

        // Create a second pane
        vfm = new VerticalFieldManager();
        for(int i = 0; i < 30; i++)
        {
            vfm.add(new LabelField("Data " + i, Field.FOCUSABLE));
        }
        LabelField iconTextLabelField = new LabelField("Pane 2");            
        model.addPane(new Pane(iconTextLabelField, vfm));

        // Create a third pane            
        vfm = new VerticalFieldManager();
        ButtonField button = new ButtonField("Button", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
        button.setChangeListener( new FieldChangeListener()
        {
            public void fieldChanged(Field field, int context)
            {
                Dialog.inform("Button activated.");
            }
        });
        vfm.add(button);
        model.addPane(new Pane(new LabelField("Pane 3"), vfm));

        // Choose which pane the model is displaying
        model.setCurrentlySelectedIndex(1);                    

        // Create the header and initialize the model and visual properties
        TitleView header = null;
        PaneManagerController controller = null;                       
        if(headerType == OptionDialog.SCROLL_HEADER_TYPE)
        {
            header = new HorizontalScrollableTitleView(Field.FOCUSABLE);
            controller = new HorizontalScrollableController();
        }
        else if(headerType == OptionDialog.TAB_HEADER_TYPE)
        {
            header = new HorizontalTabTitleView(Field.FOCUSABLE);
            ((HorizontalTabTitleView)header).setNumberOfDisplayedTabs(model.numberOfPanes());
            controller = new HorizontalTabController();
        }
        else
        {
            throw new IllegalStateException("Header type is not valid.");
        }

        header.setModel(model);
        XYEdges edgesFour = new XYEdges(4, 4, 4, 4);
        header.setBorder(BorderFactory.createRoundedBorder(edgesFour));

        // Set arrow images
        Bitmap leftArrow  = Bitmap.getBitmapResource("leftArrow.png");
        Bitmap rightArrow = Bitmap.getBitmapResource("rightArrow.png");            
        if(leftArrow != null)
        {
            header.setLeftArrow(leftArrow);
        }
        if(rightArrow != null)
        {
            header.setRightArrow(rightArrow);
        }           

        // Create the PaneView object, which will display the panes and is
        // controlled by the model.
        PaneView paneView = new PaneView(Field.FOCUSABLE);            
        paneView.setBorder(BorderFactory.createSimpleBorder(edgesOne));
        paneView.setModel(model);

        // Initialize the PaneManagerView
        PaneManagerView view = new PaneManagerView(Field.FOCUSABLE, header, paneView);
        view.setModel(model);            
        view.setBorder(BorderFactory.createRoundedBorder(edgesFour));
        model.setView(view);

        // Initialize the Controller
        controller.setModel(model);
        controller.setView(view);
        model.setController(controller);
        view.setController(controller);

        add(view);
    }
}  
}

1 个答案:

答案 0 :(得分:0)

屏幕中只有一个字段可以同时保持焦点,因此您需要通过其他方式指示哪个选项卡包含焦点字段。例如通过将选定的选项卡绘制成不同的颜色。