gpt4 book ai didi

java - 为android webview java添加进度条

转载 作者:行者123 更新时间:2023-12-01 09:10:45 25 4
gpt4 key购买 nike

请我在android开发方面非常新,我有我的网站,我在webview中加载并且现在正在工作我想添加一个进度条来显示链接何时处理以渲染并在页面加载后隐藏进度条。请我看到很多例子,但我在尝试实现它时不断遇到错误,请有人帮助我使用我自己的 webview 例如。

package com.mypage.mypage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class SourceProjectActivity extends AppCompatActivity {

private WebView projectLoaderView;
private ProgressBar progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_source_project);
projectLoaderView = (WebView)findViewById(R.id.sourcejailview);
WebSettings webSettings = projectLoaderView.getSettings();

progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);

webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
//local offline loader
webSettings.setCacheMode( WebSettings.LOAD_DEFAULT );

projectLoaderView.loadUrl("http://google.com");
projectLoaderView.setWebViewClient(new WebViewClient());
SourceProjectActivity.this.progress.setProgress(0);
}

private class MyWebViewClient extends WebViewClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
//super.onProgressChanged(view, newProgress);
}
}

public void setValue(int progress) {
this.progress.setProgress(progress);
}

@Override
public void onBackPressed(){
if(projectLoaderView.canGoBack()){
projectLoaderView.goBack();
}else {
super.onBackPressed();
}
}

}

这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app=""
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_source_project"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypage.mypage.SourceProjectActivity">

<WebView
android:id="@+id/sourcejailview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >

<ProgressBar
style="@android:style/Widget.Material.Light.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_x="124dp"
android:layout_y="43dp"
android:id="@+id/progressBar"
android:layout_height="wrap_content" />
</WebView>
</RelativeLayout>

最佳答案

WebViewClient 没有 onProgressChanged() 方法。相反,您可以使用 WebChromeClient 并设置进度条,如下所示:

private class MyWebViewClient extends WebViewClient {
public void onProgressChanged(WebView view, int newProgress) {
progress.setProgress(newProgress);
//Maybe you want to hide it once it reaches 100
if (newProgress == 100){
progress.setVisibility(ProgressBar.GONE);
}
}
}

关于java - 为android webview java添加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40937389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com