gpt4 book ai didi

java - 显示 2 个 Activity 之间的进度条

转载 作者:行者123 更新时间:2023-11-30 08:43:55 27 4
gpt4 key购买 nike

我有 2 个 Activity ,一个叫 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);
}
}

最佳答案

您可以在默认可见的第二个 Activity XML 中添加一些默认 View ,如进度条。当您加载数据或您所做的任何事情时,将其设置为 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"/>

然后在您的 Activity onCreate() 方法中:

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

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

 loader.setVisibility(View.GONE);

关于java - 显示 2 个 Activity 之间的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com