2010-08-07 30 views
1

誰能告訴我如何在Blackberry中創建一個URL鏈接,它在被點擊後打開一個網頁?如何在黑莓程序中創建一個URL

+3

什麼了你試試?你有什麼問題? – Oded 2010-08-07 08:11:36

+0

我是黑莓新手,我正在尋找顯示url鏈接的代碼,它應該在點擊它後打開網頁..我嘗試了一些代碼,但它不起作用... – user413715 2010-08-07 08:20:41

+1

再次:什麼代碼你試過了,它究竟是如何失敗的? – 2010-08-07 17:05:00

回答

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