在webview加载过程中如何防止点击网页?

时间:2015-11-26 10:37:15

标签: android webview webpage

我在我的Android应用中创建了一个webview,具有以下功能:

public class WebPageOpener extends Activity {
    private WebView webView;

    @SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        Bundle extras = getIntent().getExtras();
        String url = extras.getString("url");

        webView = (WebView) findViewById(R.id.webView);

        webView.setWebViewClient(new MyBrowser());

        // settings
        WebSettings webSettings = webView.getSettings();
        webSettings.setSaveFormData(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        webView.loadUrl(url);
    }

    private class MyBrowser extends WebViewClient {
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.ProgressBar);

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
           return true;
        }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        Log.d("TAG", url);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.d("TAG", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
    }

    public void onPageFinished(WebView view, String url) {
        progressBar.setVisibility(View.GONE);
    }
    }
}

以下是相关的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"/>

    <ProgressBar
        android:id="@+id/ProgressBar"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:visibility="gone"/>
</RelativeLayout>

我会避免用户可以点击网页内的任何按钮或图片,直到网页完成加载。换句话说,只有当进度条消失时,用户才能与网页进行交互。无论如何?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。诀窍是将进度条转换为RelativeLayout而不是ProgressBar,然后将其单击侦听器设置为WebViewClient类的null方法中的onPageStarted()

这是我的工作解决方案:

private class MyBrowser extends WebViewClient {
    final RelativeLayout progressBar = (RelativeLayout) findViewById(R.id.progressBarLayout);

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        Log.d("TAG", url);
        progressBar.setOnClickListener(null);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.d("TAG", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
    }

    public void onPageFinished(WebView view, String url) {
        progressBar.setVisibility(View.GONE);
    }
}

以下是相关的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"/>

    <RelativeLayout
        android:id="@+id/progressBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</RelativeLayout>

您可以看到我的更改,比较我在问题中发布的代码。无论如何它只是两排......