我想阻止我的Webview上的链接,我的代码是这样的:
public class WebMy extends Activity {
private WebView mWebview ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pantalla);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mWebview = new WebView(this);
mWebview.setWebViewClient(new WebViewClient());
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview .loadUrl("http://www.myweb.com");
setContentView(mWebview );
}
我看到另一个使用此代码解决问题的问题:
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.google.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// reject anything other
return true;
}
}
但我不知道如何在我的代码中使用。例如,如果我想阻止此网址http://www.myweb.com/pepito
,我该如何使用此代码执行此操作?谢谢
答案 0 :(得分:2)
尝试以下代码。
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://www.myweb.com/pepito")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// reject anything other
return true;
}
}
答案 1 :(得分:1)
单击链接“http://www.myweb.com/pepito”时将不会发生任何事情
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
return url.equals("http://www.myweb.com/pepito");
}
}
答案 2 :(得分:0)
webView.setWebViewClient(new myWebClient());
// add this while initializing webview
// then do add following code
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
// super.onPageStarted(view, url, favicon);
if (Uri.parse(url).getHost().contains("https://qa.mstitute.com/test/build/#!/")) {
// This is my web site, so do not override; let my WebView load the page
Log.d("web","block");
}else{
super.onPageStarted(view, url, favicon);
}
Log.d("web","Started");
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (Uri.parse(url).getHost().equals("https://qa.mstitute.com/test/build/#!/")) {
// This is my web site, so do not override; let my WebView load the page
Log.d("web","block");
return false;
}else {
view.loadUrl(url);
return true;
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d("web","got an error");
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
}
}
答案 3 :(得分:-1)
解决方案 1
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView : WebView = findViewById(R.id.webView)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
// This is my web site, so do not override; let the WebView load the page.
if (Uri.parse(url).host == "google.com") return false
// Reject everything else.
return true
}
}
}
}
解决方案 2
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView : WebView = findViewById(R.id.webView)
webView.webViewClient = MyWebViewClient()
}
private class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
// This is my web site, so do not override; let the WebView load the page.
if (Uri.parse(url).host == "google.com") return false
// Reject everything else.
return true
}
}
}
科特林 <3