**我正在开发一个带有webview的android应用程序。我需要在webview中加载内容时显示进度条。一旦内容加载完成,进度应该被取消。对于webview部分,我使用下面的code.i使用thread.sleep来setJavaScriptEnabled(true),因为我首先设置了getJavaScriptEnabled(false),并且在15秒后设置了setJavaScriptEnabled(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);
}
}
答案 0 :(得分:1)
您可以更简单的方式实施进度条
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);}
答案 1 :(得分:0)
试试这个..
<强>全球强>
ProgressDialog pDialog;
和 java
内pDialog = ProgressDialog.show(Activity1.this, "Loading", "Please wait...", true);
webView.getSettings().setJavaScriptEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webview.setWebViewClient(new WebViewController());
<强> WebViewController 强>
public class WebViewController extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.getSettings().setSavePassword(false);
return true;
}
public void onPageFinished(WebView view, String url) {
if(pDialog != null)
{
if(pDialog.isShowing())
pDialog.dismiss();
}
}
}
答案 2 :(得分:0)
使用Bellow Class Hope它会起作用
public class myWebViewClient extends WebViewClient {
private ProgressDialog dialog;
private String redirect_str = "Processing...\nPlease wait...";
Context context;
public MyWebViewClient(Context context) {
this.context = context;
}
@Override
public void onPageStarted(WebView paramWebView, String paramString, Bitmap paramBitmap) {
super.onPageStarted(paramWebView, paramString, paramBitmap);
if(this.dialog == null) {
this.dialog = ProgressDialog.show(this.context, "", this.redirect_str, true);
}
}
@Override
public void onPageFinished(WebView paramWebView, String paramString) {
super.onPageFinished(paramWebView, paramString);
if (this.dialog != null) {
this.dialog.dismiss();
this.dialog = null;
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String paramString) {
view.loadUrl(paramString);
return true;
}
}
答案 3 :(得分:0)
//我使用过这个课,我的代码在我这边工作,请尝试这可能会对你有所帮助
public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
private String url_string="http://www.google.com";
private String header_maintext_string="Your text";
/** Called when the activity is first created. */
@SuppressLint("SetJavaScriptEnabled") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_layout);
webview = (WebView)findViewById(R.id.webview01);
header_maintext= (TextView)findViewById(R.id.header_maintext);
header_maintext.setText(header_maintext_string);
headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
headeroptiontext.setVisibility(View.GONE);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
back = (RelativeLayout) findViewById(R.id.back_layout);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(webview.canGoBack() == true)
{
webview.goBack();
}
else
{
finish();
}
}
});
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewActivity.this, "My application", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(url_string);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}