如何在Blackberry程序中创建URL

时间:2010-08-07 08:09:17

标签: blackberry

有人能告诉我如何在Blackberry中创建一个URL链接,它会在点击后打开一个网页吗?

1 个答案:

答案 0 :(得分:1)

// This can eb a nested class in your screen class. 
class URLButtonField extends ButtonField {  
    String url; 
    public URLButtonField(String label, String url) { 
        super(label); 
        this.url = url; 
    } 
    public String getURL() { 
        return url; 
    } 
    public void setURL(String url) { 
        this.url = url; 
    } 
}    



// member variable of your screen class- this will let you access it later 
// to change the URL
URLButtonField bf;

// In your screen's constructor: 

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);

然后,您可以随时更改“bf”的文本或目标网址,无论您将其更改为什么,都将是点击它时启动的网址:

// In response to some activity: 
bf.setText("Example Two"); 
bf.setURL("http://www.example.com/two");