如何在同一个webview Android中加载webview链接 [英] How to load webview links inside this same webview Android
问题描述
我想构建一个没有PhoneGap的Android webApplication,我有一个问题:当我点击一个链接时,它会打开android默认浏览器......:那么如何在同一个webview Android中加载webview链接?
I want to build a Android webApplication without PhoneGap, and i have a problem : when i click on a link it open the android default browser... : so how to load webview links inside this same webview Android ?
这是我的代码:
package com.example.mobilewebview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
//import android.webkit.WebViewClient;
import android.webkit.WebSettings;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://m.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
谢谢!
推荐答案
您必须拦截 WebView
可能具有的URL链接才能进行自定义实施。
You have to intercept the URL links that your WebView
might have in order to do your custom implementation.
您必须为 WebView
创建 WebViewClient
然后重写 public boolean shouldOverrideUrlLoading(WebView view,String url)
方法来实现这样的事情。
You have to create a WebViewClient
for your WebView
and then override the public boolean shouldOverrideUrlLoading(WebView view, String url)
method to achieve such a thing.
示例代码:
//web view client implementation
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//do whatever you want with the url that is clicked inside the webview.
//for example tell the webview to load that url.
view.loadUrl(url);
//return true if this method handled the link event
//or false otherwise
return true;
}
}}
//in initialization of the webview:
....
webview.setWebViewClient(new CustomWebViewClient());
....
告诉我是否有帮助。
这篇关于如何在同一个webview Android中加载webview链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!