0

这是我刚刚在网上搜索的java代码。我不知道如何在进度条中放置和意图,以便在加载后可以更改布局...如果您能帮助我,那就太好了...谢谢...

public class Support extends Activity {
private static int progress;
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.support);

    progress = 0;
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setMax(400);

    while(progressStatus < 100)
    {
        progressStatus = doSomeWork();
        handler.post(new Runnable()
        {
            public void run(){
                progressBar.setProgress(progressStatus);

            }
        });

    }
    handler.post(new Runnable()
    {
        public void run()
        {
            progressBar.setVisibility(0);
        }
    });

}

private int doSomeWork() {
    try{
        Thread.sleep(50);
}       
catch(InterruptedException e)
{
    e.printStackTrace();

}
    return ++progress;
}

}

4

2 回答 2

1

首先,while 循环冻结了 UI。您需要将其移至工作线程或异步任务。

其次,进度条仅用于显示冗长任务的进度 - 而不是启动活动。要解决您的问题,请startActivity() 在循环完成时调用。

于 2013-03-21T17:43:05.890 回答
0

线程(注意 UI 冻结)

完整的 XML

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/btnStartProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Download File" />

</LinearLayout>

完整代码

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
    btnStartProgress.setOnClickListener(
             new OnClickListener() {

       @Override
       public void onClick(View v) {

        // prepare for a progress bar dialog
        progressBar = new ProgressDialog(v.getContext());
        progressBar.setCancelable(true);
        progressBar.setMessage("File downloading ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

        //reset progress bar status
        progressBarStatus = 0;

        //reset filesize
        fileSize = 0;

        new Thread(new Runnable() {
          public void run() {
            while (progressBarStatus < 100) {

              // process some tasks
              progressBarStatus = doSomeTasks();

              // your computer is too fast, sleep 1 second
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }

              // Update the progress bar
              progressBarHandler.post(new Runnable() {
                public void run() {
                  progressBar.setProgress(progressBarStatus);
                }
              });
            }

            // ok, file is downloaded,
            if (progressBarStatus >= 100) {

                // sleep 2 seconds, so that you can see the 100%
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // close the progress bar dialog
                progressBar.dismiss();
            }
          }
           }).start();

           }

            });

    }

// file download simulator... a really simple
public int doSomeTasks() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;
        } else if (fileSize == 200000) {
            return 20;
        } else if (fileSize == 300000) {
            return 30;
        }
        // ...add your own

    }

    return 100;

}

}

支持文章

于 2013-03-21T18:07:05.787 回答