【问题标题】:Not getting progress bar in webview没有在webview中获得进度条
【发布时间】:2014-04-17 04:23:18
【问题描述】:

这是我用来在 webview 中显示进度条的代码。请更正我的代码,以显示水平样式进度条以跟踪 WebVIew 完成加载 URL 的时间,每次用户单击链接时显示进度条,添加代码以显示进度条。

package com.pskpartha.droidwebview;

import com.pskpartha.droidwebview.info.Info;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.PluginState;
import android.widget.ProgressBar;

public class DroidWebViewActivity extends Activity {
/** Called when the activity is first created. */
Context con;
private WebView fweBview;
private WebSettings webSettings;
ProgressBar progressBar;

String url = Info.WebUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    con = this;
    try {
        if (!SharedPreferencesHelper.isOnline(con)) {
            AlertMessage.showMessage(con, "", "No internet connection");
            return;
        }
        updateWebView(url);

    } catch (Exception e) {
        // TODO: handle exception
    }
}


private class HelloWebViewClient extends WebViewClient {

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

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && fweBview.canGoBack()) {
        fweBview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);

}
public class AppWebViewClients extends WebViewClient {
     private ProgressBar progressBar;

    public AppWebViewClients(ProgressBar progressBar) {
        this.progressBar=progressBar;
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
}
private void updateWebView(String url) {
    // TODO Auto-generated method stub
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    fweBview = (WebView) findViewById(R.id.fbwebView);
    fweBview.getSettings().setJavaScriptEnabled(true);
    fweBview.getSettings().setDomStorageEnabled(true);
    fweBview.getSettings().setPluginState(PluginState.ON);
    webSettings = fweBview.getSettings();

    webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
    webSettings.setBuiltInZoomControls(true);

    fweBview.loadUrl(url);

    fweBview.setWebViewClient(new HelloWebViewClient());

}

// /---- Rating Button-----------------------------

public void btnRating(View v) {

    try {

        alertbox(null, null);

    } catch (Exception e) {
        // TODO: handle exception
    }

}

protected void alertbox(String title, String mymessage) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Encourage us By Rating Our App").setCancelable(
            false).setTitle("").setPositiveButton("Rate Now",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // finish the current activity
                    // AlertBoxAdvance.this.finish();
                    /*
                     * Intent myIntent = new Intent(
                     * Settings.ACTION_SECURITY_SETTINGS);
                     * startActivity(myIntent);
                     */
                    Intent viewIntent = new Intent(
                            "android.intent.action.VIEW",
                            Uri
                                    .parse(Info.GooglePlayAppUrl));
                    startActivity(viewIntent);
                    dialog.cancel();
                }
            }).setNegativeButton("Later",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // cancel the dialog box

                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

// ------------ Share button---------------------

public void btnShare(View v) {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "    ");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "shareBody");
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

// ------------ Home button---------------------
public void btnHome(View v) {
    try {
        updateWebView(url);
    } catch (Exception e) {
        // TODO: handle exception
    }

    // Intent i = new Intent(DroidWebViewActivity.this,
    // DroidWebViewActivity.class); // your class
    // startActivity(i);

}

// ------------ Email button---------------------
public void btnEmail(View v) {

    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL,
            new String[] { "" });
    email.putExtra(Intent.EXTRA_SUBJECT, Info.eMailSubject);
    email.putExtra(Intent.EXTRA_TEXT, Info.eMailDetails);
    email.setType("message/rfc822");
    startActivity(Intent.createChooser(email, "Choose an Email client :"));

}

// ------------ Call button---------------------
private void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:"+Info.phoneNumber+""));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {

    }
}

public void btnPhone(View v) {

    try {

        call();

    } catch (Exception e) {
        // TODO: handle exception
    }

}
}


 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="9"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="vertical" >

            <ProgressBar
                android:id="@+id/progressBar12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />


    </RelativeLayout>

    <WebView
        android:id="@+id/fbwebView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

</LinearLayout>'

【问题讨论】:

  • Progress Dialog代替Progress Bar
  • 你也可以发布你的xml吗?

标签: android webview android-webview webviewclient


【解决方案1】:

这是修改后的 Java 文件和 XML..

Java 文件是DroidWebViewActivity

还有 XML 文件...XML File

希望它对你有用。

谢谢

【讨论】:

  • 它显示的错误 - ll_web_view 无法解析或不是字段
  • ll_web_view 这个xml文件的名字.u可以用你的xml文件名改变..@user3543740
  • 我仍然没有让进度条工作看到这里 - link
  • 在加载 google.com 之前你没有看到进度条??@user3543740
  • 每次点击链接时都需要加载栏
【解决方案2】:

请删除

requestWindowFeature(Window.FEATURE_NO_TITLE);

并添加

getWindow().requestFeature(Window.FEATURE_PROGRESS);

之前

setContentView(R.layout.main);

加载时它会在屏幕顶部显示进度条 网址。 希望它能解决你的问题...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
相关资源
最近更新 更多