在Android中,当WebView加载URL时,如何显示进度条? [英] How to display a progress bar when WebView loads a URL, in Android ?

查看:58
本文介绍了在Android中,当WebView加载URL时,如何显示进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android应用程序开发和学习的新手.

我开发了一个使用 WebView 加载网站链接的应用程序.而且工作正常.

I have developed an application which loads a website link using a WebView. And it's working fine.

但是当 WebView 加载页面时,我在显示和隐藏进度栏时遇到了问题.

But I am facing problems to display and hide progress bar when the WebView loads a page.

我想在WebView加载网站的主链接和内部链接时显示进度条(仅微调器位于中心),并在页面完全加载时隐藏进度条.

I want to display a progress bar (Just spinner in center) when the WebView loads the main link and internal links of a website and hide progress bar when page is completely loaded.

我的Java代码:

package in.matebook.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.labnol.org");
    }



    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (Uri.parse(url).getHost().equals("www.labnol.org")) {
                // This is my web site, so do not override; let my WebView load the page
                //or we can add more allowed host in if condition
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我的布局XML代码:

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


    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

现在,进度条会显示,但不会隐藏.

Right now, the progress bar is displayed, but it does not hide.

当我创建 progressbar 对象并在Java代码中添加以下行时,进度条不会显示.

When I create progressbar object and add below line in Java code then the progress bar is not displayed.

progressbar.setVisibility(View.GONE);

我还尝试了其他答案,但是当我单击内部链接时,进度栏不起作用.

I have also tried an other answer but the progress bar does not work when I click on internal links.

  1. 我想在加载主页时显示进度条,并在完全加载主页时隐藏进度条.

  1. I want to display progress bar when main page load and hide progress bar when the main page is completely loaded.

我想在用户单击网站的内部链接时再次显示进度条,与上面相同,在页面完全加载时隐藏进度条.

I want to display the progress bar again when the user clicks on internal links of the website and same as above, hide the progress bar when the page is completely loaded.

请帮助,我是Android应用程序开发的初学者.

Please help, I am beginner in Android apps development.

推荐答案

尝试此代码...您需要webChoromeClient来跟踪webview的加载进度...

try this code... you need webChoromeClient to track the webview load progress...

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
    progressBar.setProgress(progress);
    if (progress == 100) {
        progressBar.setVisibility(View.GONE);

    } else {
        progressBar.setVisibility(View.VISIBLE);

    }
 }
});

用此代码替换您的webview客户端....

Replace your webview client with this code....

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
   }
}

这篇关于在Android中,当WebView加载URL时,如何显示进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆