如何在WebView内部加载外部网页

我的问题是,网页没有加载在web视图内。

mWebview.loadUrl("http://www.google.com"); 启动网页浏览器…

这是我的活动的代码:

 import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class Main extends Activity { private WebView mWebview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mWebview = new WebView(this); mWebview.loadUrl("http://www.google.com"); setContentView(mWebview); } } 

我在清单中添加了所需的权限:

 <uses-permission android:name="android.permission.INTERNET" /> 

感谢这篇文章 ,我终于find了解决办法。 这里是代码:

 import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class Main extends Activity { private WebView mWebview ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mWebview = new WebView(this); mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript final Activity activity = this; mWebview.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); } }); mWebview .loadUrl("http://www.google.com"); setContentView(mWebview ); } } 

尝试这个

webviewlayout.xml:

 <?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/help_webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none" /> 

在您的活动中:

 WebView webView; setContentView(R.layout.webviewlayout); webView = (WebView)findViewById(R.id.help_webview); webView.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.google.com"); 

更新

添加webView.setWebViewClient(new WebViewController()); 到您的活动。

WebViewController类:

 public class WebViewController extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } 
 public class WebViewController extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } webView.setWebViewClient(new WebViewController()); 

请使用此代码: –

main.xml中

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/top_heading" android:id="@+id/rlayout1"> <TextView android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textColor="#ffffff" android:textSize="22dip" android:textStyle="bold" android:layout_height="wrap_content" android:text="More Information" android:id="@+id/txtviewfbdisplaytitle" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/rlayout1" android:id="@+id/rlayout2"> <WebView android:id="@+id/webview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" /> </RelativeLayout> </RelativeLayout> 

MainActivity.Java

 public class MainActivity extends Activity { private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } Button btnBack; WebView webview; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); webview=(WebView)findViewById(R.id.webview1); webview.setWebViewClient(new MyWebViewClient()); openURL(); } /** Opens the URL in a browser */ private void openURL() { webview.loadUrl("http://www.google.com"); webview.requestFocus(); } } 

如果有任何查询问我,请试试这个代码。

你可以这样做。

 webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("Your URL goes here"); 

这是非常简单的尝试在Android Manifest文件中先集成这些代码行权限

 <uses-permission android:name="android.permission.INTERNET" /> 

然后在你的Activity.xml中写一些代码

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.MainActivity"> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/help_webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

然后把这些代码写在你的MainActivity.java中

 import android.app.Activity; import android.content.Intent; import android.content.res.Resources; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Window; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class MainActivity extends Activity{ private WebView mWebview ; String link = "";// global variable Resources res;// global variable @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_modernherbal_main); mWebview = (WebView) findViewById(R.id.help_webview); WebSettings webSettings = mWebview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); final Activity activity = this; mWebview.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); } }); mWebview .loadUrl("http://www.example.com"); } 

}

试试这会帮助你解决你的问题

在你的activity类中添加下面的方法。这里的浏览器不过是你的webview对象。 现在,您可以轻松地查看网页包含页面。

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) { browser.goBack(); return true; } return false; }