gpt4 book ai didi

java - 使用 webview 显示进度条

转载 作者:行者123 更新时间:2023-11-30 11:25:18 25 4
gpt4 key购买 nike

**我正在开发一个带有 webview 的 android 应用程序。我需要在 webview 中加载内容时显示进度条。内容加载完成后应关闭进度。对于 webview 部分,我使用下面的代码。我使用 thread.sleep 来设置 JavaScriptEnabled(true),因为我首先设置 JavaScriptEnabled(false),然后在 15 秒后设置 JavaScriptEnabled(true)。我应该怎么做

public class Activity1 extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity1);
Toast.makeText(getApplicationContext(), "welcome", Toast.LENGTH_LONG).show();
String url = "http://student.iaun.ac.ir";
webView = (WebView) findViewById(R.id.webview);

webView.setWebViewClient(new myWebViewClient());
webView.getSettings().setJavaScriptEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
webView.getSettings().setSavePassword(false);

new Thread(new Runnable() {

@Override
public void run() {
try {
startDelay();
webView.getSettings().setJavaScriptEnabled(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

}

void startDelay() throws InterruptedException {
Thread.sleep(15000);
}

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

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}

最佳答案

你可以用更简单的方式实现进度条

protected static ProgressBar progressBar;

//Some Codes
WebView loginwebview = (WebView) activity.findViewById(R.id.login_webview);
loginwebview.setWebChromeClient
(
new WebChromeClient()
{
@Override
public void onProgressChanged(WebView view, int newProgress)
{
activity.setTitle("Loading...Please wait");
startSpinner();
activity.setProgress(newProgress * 100);
if(newProgress == 100)
{
activity.setTitle(R.string.app_name);
stopSpinner();
}
}
});

loginwebview.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;

}

以及 startSpinner() 和 stopSpinner() 方法

 protected static void startSpinner(){progressBar.setVisibility(WebView.VISIBLE);}
protected static void stopSpinner(){progressBar.setVisibility(WebView.GONE);}

关于java - 使用 webview 显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20327774/

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