2016-07-02 105 views
-2

任何人都可以幫助我添加進度條嗎?
我已將各種進度欄方法添加到我的代碼中,但進度欄並未在成功加載頁面後結束。我需要我的應用代碼進度條代碼

這是我MainActivity.java:

package com.myappname.app; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

import com.pushbots.push.Pushbots; 

public class MainActivity extends Activity { 
    private WebView mWebView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Pushbots.sharedInstance().init(this); 

     mWebView = (WebView) findViewById(R.id.activity_main_webview); 

     // Force links and redirects to open in the WebView instead of in a browser 
     mWebView.setWebViewClient(new WebViewClient()); 

     // Enable Javascript 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     // Use remote resource 
     mWebView.loadUrl("http://www.mysiteurl.com"); 

     // Stop local links and redirects from opening in browser instead of WebView 
     mWebView.setWebViewClient(new MyAppWebViewClient()); 

     // Use local resource 
     // mWebView.loadUrl("file:///android_asset/www/index.html"); 
    } 

    // Prevent the back-button from closing the app 
    @Override 
    public void onBackPressed() { 
     if(mWebView.canGoBack()) { 
      mWebView.goBack(); 
     } else { 
      new AlertDialog.Builder(this) 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .setTitle("Alert") 
        .setMessage("Are you sure you want exit?") 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
        { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          finish(); 
         } 

        }) 
        .setNegativeButton("No", null) 
        .show(); 
     } 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

這是我在佈局activity_main.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:id="@+id/activity_main_webview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

回答

0

建議使用內部的AsyncTask進度對話框。 所以首先聲明一個進度對話框:

ProgressDialog progressDialog; 

初始化:

this.progressDialog = new ProgressDialog(context); 

內。然後PreExecute:

progressDialog.setMessage("Loading"); 


progressDialog.show(); 

和內部PostExecute:

progressDialog.dismiss(); 
+0

因爲這是我的第一款應用如此我沒有太多的知識,你能否在我的上面的代碼中實現進度條碼? –

+0

您必須先實施AsyncTask。試試這個https://developer.android.com/reference/android/os/AsyncTask.html – Akriti

+0

只是一個例子,你可以看看這個:https://github.com/akritikts/TravelPlanner/tree/master/應用程序/ src目錄/主/ JAVA /中/ silive/travelplanner /酒店 – Akriti