Android WebView从WebViewClient进行通信到Activity

时间:2010-07-14 21:07:02

标签: android webviewclient

我有一个简单的helloworld应用程序。我试图告诉活动用户点击了“Cnn.com”

    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背景)

1 个答案:

答案 0 :(得分: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();
    }
}