(Android Studio)当页面加载完成后,Progress Dialogue不会消失

时间:2017-10-02 08:26:50

标签: android

我为我的博客构建了一个 WebView应用程序。除了一件事,一切都运行良好。 主页加载时,屏幕上显示的 ProgressDialog 不会消失。即使在网页加载完毕后也是如此。

然而,在点击屏幕两次后它确实消失了...........任何帮助都会受到高度赞赏。

我对 Android 开发很陌生,因此,对于学习的答案,一点解释会有所帮助。

Ps Im使用

minSdkVersion 21
targetSdkVersion 26

我的Java代码是:

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView = new WebView(this);
        webView.setClickable(true);
        webView.setFocusableInTouchMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        WebClientClass webViewClient = new WebClientClass();
        webView.setWebViewClient(webViewClient);

        setContentView(webView);
    }

    public class WebClientClass extends WebViewClient {
        ProgressDialog pd = null;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            pd = new ProgressDialog(MainActivity.this);
            pd.setTitle("Please wait");
            pd.setMessage("Page is loading..");
            pd.show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            pd.dismiss();
        }
    }


}

我的xml代码是:

<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
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webview"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

我的应用清单代码是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.notherstore.notherrstore">

    <uses-permission android:name="android.permission.INTERNET" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >

            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

2 个答案:

答案 0 :(得分:1)

多次调用

onPageStarted(),因此您的 ProgressDialog 也会多次凝视。 从我在这篇文章中找到的内容:https://stackoverflow.com/a/25547544/5223744

如果 onPageStarted()方法启动 onPageFinished()网址正常,但网址 onPageStarted()启动shouldOverrideUrlLoading然后 onPageFinished()之后重定向。

  

只需在 onPageStarted()中放置一个breakPoint,您就会看到   发生。

您可以设置一个布尔值来检查 onPageStarted() isFirstTime,或者您可以使用以下代码:

只需更改 MainActivity 类,如下所示:

public Row CopyLine(SpreadsheetDocument excelDoc, string sheetName, int srcRowIdx, int refRow)
            {
                var worksheetPart = GetWorksheetPart(excelDoc, sheetName);
                var sheetData = worksheetPart.Worksheet.Descendants<SheetData>().First();
                var srcRow = GetRow(worksheetPart, srcRowIdx);
                var clonedRow = (Row)srcRow.CloneNode(true);
                var rowRef = GetRow(worksheetPart, refRow);

                //Update all indexes to index+1
                IEnumerable<Row> rows = sheetData.Descendants<Row>().Where(r => r.RowIndex.Value >= refRow);
                foreach (Row row in rows)
                {
                    var clonedRowIndex = Convert.ToUInt32(row.RowIndex.Value + 1);

                    foreach (Cell cell in row.Elements<Cell>())
                    {
                        // Update the references for reserved cells.
                        string cellReference = cell.CellReference.Value;
                        cell.CellReference =
                            new StringValue(cellReference.Replace(row.RowIndex.Value.ToString(), clonedRowIndex.ToString()));
                    }
                    // Update the row index.
                    row.RowIndex = new UInt32Value(clonedRowIndex);
                }

                sheetData.InsertBefore(clonedRow, rowRef);

                worksheetPart.Worksheet.Save();
                return clonedRow;
            }

答案 1 :(得分:0)

如果要在页面加载完成之前保持进度对话框,只需设置对话框的可取消错误,所以:

    pd.setCancelable(false);
相关问题