我最近为我的android工作室游戏应用程序创建了一个进度条,它移动正常,但我希望它在进度条达到100时启动第二个活动。我不知道放在哪里,我不知道&# 39;我想犯错,然后必须重新开始我的代码。所以有人可以帮我做什么?这是我的MainActivty.Java文件(这是进展的地方)。
package com.example.brandon.territories;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressStatus = 0;
private ImageView ImageView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
//Long operation by thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 4;
//Update progress bar with completion of operation
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
// Sleep for 300 milliseconds.
//Just to display the progress slowly
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
class MyProgressBar extends ProgressBar
{
public MyProgressBar(Context context) {
super(context);
}
@Override
public void setProgress(int progress)
{
super.setProgress(progress);
if(progress == this.getMax())
{
//Do stuff when progress is max
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items
//to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
许多方法,您只需创建ProgressBar的自定义实现并覆盖setProgress方法:
class MyProgressBar extends ProgressBar
{
@Override
public void setProgress(int progress)
{
super.setProgress(progress);
if(progress == this.getMax())
{
//Do stuff when progress is max
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
}
}
}
只需将以上代码段添加到您的MainActivity即可享受乐趣。
答案 1 :(得分:0)
为什么不使用简单的AsyncTask并从后台任务结束时调用的onPostExecute方法启动新的Activity?
一个例子:
AsyncTask<Void, Integer, Void> myAsyncTask = new AsyncTask<Void, Integer, Void>() {
@Override
protected Void doInBackground(Void... params) {
Log.d(TAG, "doing some work in background");
try {
for (int i = 0; i <= 100; i++) {
Thread.sleep(50);
publishProgress(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.d(TAG, "update progress bar here: " + values[0]);
}
@Override
protected void onPostExecute(Void result) {
Log.d(TAG, "finishing background work, can start activity here");
}
};
myAsyncTask.execute();
答案 2 :(得分:0)
只需替换
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
由:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
答案 3 :(得分:0)
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private ProgressBar pgsBar;
private int i = 0;
private TextView txtView;
private Handler hdlr = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pgsBar = (ProgressBar) findViewById(R.id.progressBar);
txtView = (TextView) findViewById(R.id.textView);
i = pgsBar.getProgress();
new Thread(new Runnable() {
public void run() {
while (i < 100) {
i += 1;
// Update the progress bar and display the current value in text view
hdlr.post(new Runnable() {
public void run() {
pgsBar.setProgress(i);
txtView.setText(i+"%");
// or txtView.setText(i+"/"+pgsBar.getMax());
if( i == 100)
{
//Do stuff when progress is max
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
});
try {
// Sleep for 100 milliseconds to show the progress slowly.
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
/*
class MyProgressBar extends ProgressBar
{
public MyProgressBar(Context context) {
super(context);
}
@Override
public void setProgress(int progress)
{
super.setProgress(progress);
if(progress == this.getMax())
{
//Do stuff when progress is max
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
}
*/
}
那样简单!
if( i == 100)
{
//Do stuff when progress is max
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}