【问题标题】:How to make HyperLink in BlackBerry application?如何在黑莓应用程序中制作超链接?
【发布时间】:2009-10-21 13:06:57
【问题描述】:

谁能告诉我如何在 BlackBerry 中制作超链接?

【问题讨论】:

    标签: user-interface blackberry java-me custom-controls


    【解决方案1】:

    这是一个看起来像超链接的自定义标签字段,可以打开一个新屏幕。我们称它为 SampleOpenScreenHyperLinkField,我们也将其添加为上下文或完整菜单项。

    • 创建应用入口点
    • 创建示例屏幕并添加自定义标签字段

    首先是入口点。

    package sample;
    
    import net.rim.device.api.ui.UiApplication;
    
    /**
     * The entry point to application startup.
     */
    public class SampleApp extends UiApplication {
    
        /**
         * The entry point.
         * 
         * @param args
         */
        public static void main(String[] args) {
            // Create a new instance of the application
            // and start the application on an event thread
            SampleApp app = new SampleApp();
    
            app.enterEventDispatcher();
        }
    
        /**
         * Basic constructor.
         */
        public SampleApp() {
            UiApplication.getUiApplication().pushScreen(new SampleScreen(1));
        }
    }
    

    接下来,您的示例屏幕。

    package sample;
    
    import net.rim.device.api.ui.Manager;
    import net.rim.device.api.ui.component.RichTextField;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class SampleScreen extends MainScreen {
    
    
        public SampleScreen(int screenNumber) {
    
            super(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
            this.add(new RichTextField("Screen " + screenNumber));
            int nextScreenNumber = screenNumber +1; 
            this.add(new SampleOpenScreenHyperLinkField("Screen " + nextScreenNumber, nextScreenNumber));
        }
    }
    

    现在,为您的自定义标签字段。

    package sample;
    import net.rim.device.api.ui.ContextMenu;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.FontFamily;
    import net.rim.device.api.ui.MenuItem;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.component.LabelField;
    
    /**
     * 
     * @author chad.lafontaine
     *
     */
    public class SampleOpenScreenHyperLinkField extends LabelField {
    
        private int pageCount = -1;
        private MenuItem mGetLinkMenuItem;
        /**
         * 
         * @param hyperlinkLabel - label to display link
         */    
        public SampleOpenScreenHyperLinkField(String hyperlinkLabel, int pageCount) {
            super(hyperlinkLabel, FOCUSABLE);
            this.pageCount = pageCount;
            Font font = getBasefontSize(10);
            setFont(font.derive(Font.UNDERLINED | Font.PLAIN));
            mGetLinkMenuItem = new SampleOpenScreenMenuItem(hyperlinkLabel);
        }
    
        /**
         * Context menu
         */
        public ContextMenu getContextMenu() {
            // Add our "Get Link" menu item to the context menu
            ContextMenu menu = super.getContextMenu();
            menu.addItem(mGetLinkMenuItem);
            return menu;
        }
    
        /**
         * Inner class 
         */
        class SampleOpenScreenMenuItem extends MenuItem {
    
            public SampleOpenScreenMenuItem(String menuLabel) {
                super(menuLabel, 0, 100);
            }
    
            public void run() {
                UiApplication.getUiApplication().pushScreen(new SampleScreen(pageCount));
    
            }
    
        }
    
        /**
         * Base font of the application. Method is static for other UI components to
         * call for calculation of display fields.
         * 
         * @return
         */
        public static Font getBasefontSize(int size) {
            Font baseFont = null;
            try {
                baseFont = FontFamily.forName("BBClarity").getFont(FontFamily.SCALABLE_FONT, size);
            } catch (ClassNotFoundException e) {
                baseFont = Font.getDefault().getFontFamily().getFont(FontFamily.SCALABLE_FONT, size);
            }
    
            return baseFont;
        }
    }
    

    结果:您应该有一个带有“超链接”的屏幕,指向另一个屏幕。如果您想调用浏览器怎么办?您可以为此使用以下类。

    package sample;
    
    import net.rim.blackberry.api.browser.Browser;
    import net.rim.device.api.ui.ContextMenu;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.FontFamily;
    import net.rim.device.api.ui.MenuItem;
    import net.rim.device.api.ui.component.LabelField;
    
    /**
     * Custom action label to invoke a browser given a URL.
     */
    public class InvokeBrowserHyperlinkField extends LabelField {
        private String mUrl;
        private MenuItem mGetLinkMenuItem;
    
        /**
         * Constructs a new HyperlinkField instance with provided label, style and
         * URL
         * 
         * @param label
         *            Label string to be displayed.
         * @param style
         *            Field style for the label.
         * @param url
         *            URL to be opened in the browser.
         */
        public InvokeBrowserHyperlinkField(String label, long style, String url) {
            super(label, style | FOCUSABLE);
    
            setFont(Font.getDefault().getFontFamily().getFont(FontFamily.SCALABLE_FONT, 10));
    
            mUrl = url;
    
            mGetLinkMenuItem = new MenuItem("Get Link", 0, 0) {
                public void run() {
                    // invoke browser with URL
                    Browser.getDefaultSession().displayPage(mUrl);
                }
            };
        }
    
        /**
         * Constructs a new HyperlinkField instance with provided label and url with
         * default style
         * 
         * @param label
         *            Label string to be displayed.
         * @param url
         *            URL to be opened in the browser.
         */
        public InvokeBrowserHyperlinkField(String label, String url) {
            this(label, 0, url);
        }
    
        public ContextMenu getContextMenu() {
            // Add our "Get Link" menu item to the context menu
            ContextMenu menu = super.getContextMenu();
            menu.addItem(mGetLinkMenuItem);
            return menu;
        }
    }
    

    结果:单击链接或菜单项,应调用设备浏览器到所需的 URL。

    【讨论】:

      【解决方案2】:

      我不认为有任何可用的东西,但是您可以扩展 LabelField 并通过覆盖 drawFocus 提供所需的功能,以在获得焦点时更改超链接颜色,并使用 invokeAction 来执行超链接所需的任务。

      【讨论】:

        【解决方案3】:
        URLButtonField bf;
        bf = new ButtonField("Example", "http://www.example.com"); 
        bf.setFieldChangeListener( new FieldChangeListener() { 
            void fieldChanged(Field field, int context) { 
                if (field == this) { 
                    BrowserSession session =- Browser.getDefaultSession();
                    session.displayPage(getURL()); 
                }
            } 
        } );         
        add(bf);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-29
          • 1970-01-01
          相关资源
          最近更新 更多