gpt4 book ai didi

Android WebView 从 WebViewClient 到 Activity 的通信

转载 作者:太空狗 更新时间:2023-10-29 12:58:32 26 4
gpt4 key购买 nike

我有一个简单的 helloworld 应用程序。我试图告诉 Activity 用户点击了“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 背景)

最佳答案

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

关于Android WebView 从 WebViewClient 到 Activity 的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com