You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何让WebView正常工作?

Getting Your Android WebView Fully Set Up

Hey there! It looks like you're well on your way to setting up a WebView in your Android app. You've knocked out the initial setup steps, so let's fill in the missing pieces to get it fully functional.

What You've Already Completed (Great Start!)

You've checked off the foundational steps perfectly:

  • Imported the required WebView classes:
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
  • Added the critical internet permission to your AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • Started building out the onCreate method to initialize your WebView:
    private WebView webview;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview); // Make sure your layout has a WebView with this ID!
    

Completing the WebView Configuration & Loading Logic

To make your WebView work as expected (load web pages internally instead of opening the system browser, and enable necessary features like JavaScript), here's the full onCreate implementation:

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview = (WebView) findViewById(R.id.webview);

    // Enable JavaScript if your target webpage requires it
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Set a WebViewClient to keep page navigation within the WebView
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    // Load your target webpage here
    webview.loadUrl("https://your-desired-website.com");
}

Don't Forget the Layout File!

You need to add the WebView component to your activity_main.xml layout (make sure the ID matches what you use in findViewById):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Extra Tips for Smooth Operation

  • Cleartext Traffic (Android 11+): If you're loading an HTTP (not HTTPS) webpage, add this to your AndroidManifest.xml inside the <application> tag to avoid errors:
    android:usesCleartextTraffic="true"
    
  • Lifecycle Management: Prevent memory leaks and improve performance by handling the WebView's lifecycle:
    @Override
    protected void onPause() {
        super.onPause();
        webview.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        webview.onResume();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
    }
    

内容的提问来源于stack exchange,提问作者Jay1995

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:22:58