[英]Loading web pages in webview in android?
我有一個鏈接,例如“ http://google.com”,我需要在網頁視圖或其他視圖中將網頁加載到我的應用中,而不是在android的默認瀏覽器中加載,無論是否可能不。
謝謝
我認為您想在應用程序內的webview中加載URL 。 如果我沒看錯,那么您可以:
WebView browser = (WebView)findViewById(R.id.browser);
browser.setWebViewClient(new WebViewClient() {
/* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link,
* but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
/* intercept all page load attempts and load yahoo.com instead */
String myAlternativeURL = "http://yahoo.com";
if (!url.equals(myAlternativeURL)) {
view.loadUrl(myAlternativeURL);
return true;
}
return false;
}
});
browser.loadUrl("http://google.com");
並且通常,您還可以使用webview.loadURL(URL)
方法在webview.loadURL(URL)
中加載URL。
是的,您可以使用WebView,API,webview.loadUrl(your_url); 在Android Manifest文件中提供互聯網訪問權限。
在這里檢查
http://developer.android.com/reference/android/webkit/WebView.html
檢查例如與loadUrl();
您需要擴展WebViewClient ,並在其中啟動URL。
public class WebActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = (WebView) findViewById(R.id.wv);
webview.setWebViewClient(new WebC());
webview.loadUrl("http://google.com");
}
public class WebC extends WebViewClient
{
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
... etc.
在您的布局xml中,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.