2010-07-14 67 views
3

我有一個簡單的helloworld應用程序。我試圖告訴活動,用戶點擊,「Cnn.com」Android WebView從WebViewClient通信到活動

WebViewClient wc = new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (url.equals("http://cnn.com/")) { 
       //TELL ACTIVITY CNN WAS CLICKED 
       return true; 
      } else { 
       return false; 

      } 
     } 

    }; 

    mWebView.setWebViewClient(wc); 

我該怎麼做。

(我來自一個C#.NET背景的)

+0

你想究竟是如何告訴的活動,?你想顯示一個對話框?你想打開另一個活動?你想展示一個吐司? – Cristian 2010-07-14 21:09:08

回答

3
public class YourActivity extends Activity{ 

    // bla bla bla 

    // the code you already have 
    WebViewClient wc = new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (url.equals("http://cnn.com/")) { 
       YourActivity.this.tellMe(); 
       return true; 
      } else { 
       return false; 

      } 
     } 

    }; 

    // this is the method to 'tell' the activity that someone clicked the link 
    public void tellMe(){ 
     // in this case I just raise a toast. 
     // but you can do whatever you want here ;) 
     Toast.makeText(YourActivity.this, "eyy!! somebody clicked the cnn link", 1).show(); 
    } 
} 
+0

+1 for'eyy !!有人點擊了cnn鏈接# – Pentium10 2010-07-14 21:16:49

+0

haha​​haha LOL!你是邪惡的jajaja – Cristian 2010-07-14 21:31:59

+1

我其實更喜歡BBC的新聞,但這個作品,我會給它的榮譽:) – 2010-07-15 01:00:57