2016-07-02 56 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