显示2个活动之间的进度条

问题描述 投票:2回答:2

我有2个活动,1个叫做MainActivity,另一个叫做Circle。当我单击MainActivity上的按钮以启动第二个按钮时,我想要一个进度条加载屏幕。这是我目前的代码,但它只是导致应用程序崩溃。

public class LoadingScreenActivity extends Activity 
{
//A ProgressDialog object
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();       

}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
        //Create a new progress dialog
        progressDialog = new ProgressDialog(LoadingScreenActivity.this);
        //Set the progress dialog to display a horizontal progress bar 
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //Set the dialog title to 'Loading...'
        progressDialog.setTitle("Loading...");
        //Set the dialog message to 'Loading application View, please wait...'
        progressDialog.setMessage("Loading application View, please wait...");
        //This dialog can't be canceled by pressing the back key
        progressDialog.setCancelable(false);
        //This dialog isn't indeterminate
        progressDialog.setIndeterminate(false);
        //The maximum number of items is 100
        progressDialog.setMax(100);
        //Set the current progress to zero
        progressDialog.setProgress(0);
        //Display the progress dialog
        progressDialog.show();
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        try 
        {
            //Get the current thread's token
            synchronized (this) 
            {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4)
                {
                    //Wait 850 milliseconds
                    this.wait(850);
                    //Increment the counter 
                    counter++;
                    //Set the current progress. 
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        return null;
    }

    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        //set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        //close the progress dialog
        progressDialog.dismiss();
        //initialize the View
        setContentView(R.layout.content_circle);
    }   
}

}

java android
2个回答
1
投票

首先为ProgressDialog创建一个公共的类实用程序来重用代码

public class Utility {
    public static ProgressDialog getProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context,
                R.style.TransparentDialog);
        progressDialog.setCancelable(false);
        progressDialog
                .setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        progressDialog.setProgress(0);
        return progressDialog;
    }

}

然后在您的活动或片段中使用Above类。但是你必须使用Intent来进行下一个活动。您无法直接设置下一个活动布局

public class LoadingScreenActivity extends Activity 
{
//A ProgressDialog object
protected ProgressDialog dialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();       

}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
            super.onPreExecute();
            dialog = Utility.getProgressDialog(context);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            if (dialog != null) {
                dialog.show();
            }

    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        try 
        {
            //Get the current thread's token
            synchronized (this) 
            {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4)
                {
                    //Wait 850 milliseconds
                    this.wait(850);
                    //Increment the counter 
                    counter++;
                    //Set the current progress. 
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        return null;
    }



    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        //close the progress dialog
         dialog.dismiss();
        // use intent here to go next activity
        Intent i = new Intent(this,SecondActivity.class);
        startIntent(i);

    }   
}

1
投票

您可以在默认情况下可见的第二个活动XML中添加一些默认视图,如progressbar。当您加载数据或其他任何内容时,将其设置为view.GONE。好的图书馆是这样的:

https://github.com/81813780/AVLoadingIndicatorView

在你的second_activity.xml中使用:

 <com.wang.avi.AVLoadingIndicatorView
        android:layout_gravity="center"
        android:id="@+id/avloadingIndicatorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:indicatorName="BallPulse"
        app:indicatorColor="@color/myPrimaryColor"/>

然后在你的活动onCreate()方法中:

  public void onCreate(Bundle savedInstanceState) {            
            super.onCreate(savedInstanceState);
loader= (AVLoadingIndicatorView) findViewById(R.id.avloadingIndicatorView);
    }

最后当你完成加载时,只需使用:

 loader.setVisibility(View.GONE);
© www.soinside.com 2019 - 2024. All rights reserved.