1

如何在单击活动中的按钮时显示进度条我的主要活动中有一个按钮,如果我单击该按钮我想显示一个进度条我不想为此使用辅助活动我已经完成了 2 个活动但是我需要它在主要活动本身中完成。

谢谢你

我的代码:

家庭活动:

public void load(View v){
    Intent intent = new Intent(this,Splash.class);
    this.startActivity(intent);
}

飞溅活动:

public class Splash extends Activity {
    private long ms = 0;
    private long splashTime = 20000;
    private boolean splashActive = true;
    private boolean paused = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);

        Thread mythread = new Thread() {
            public void run() {
                try {
                    while (splashActive && ms < splashTime) {
                        if (!paused)
                            ms = ms + 100;
                        sleep(100);
                    }
                } catch (Exception e) {
                } finally {
                    Intent intent = new Intent(Splash.this, Home.class);
                    startActivity(intent);
                }
            }
        };
        mythread.start();
    }
}
4

5 回答 5

9

你可以这样做

ProgressDialog progress;

progress = new ProgressDialog(this);
progress.setTitle("Please Wait!!");
progress.setMessage("Wait!!");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();

在要显示ProgressBar.

于 2013-04-23T06:29:21.293 回答
6

还有一种方法可以在单击按钮时显示进度条。

将此代码放入您的xml

 <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

只需在您的 Button 单击 java 文件中编写以下代码。

progressBar.setVisibility(View.VISIBLE);

它将在您的按钮单击上显示进度条。

于 2014-03-07T12:57:49.283 回答
2

我相信 user2306156 现在不需要这个,希望正在寻找这个的人会很高兴知道我猜。

将此代码放入您的xml中

<ProgressBar
        style="@android:style/Widget.Material.Light.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp"
        android:id="@+id/progressBar" />

把它放在你的活动代码中

spinner=(ProgressBar)findViewById(R.id.progressBar);
        spinner.setVisibility(View.GONE);

这在您的活动代码的onClick中

spinner.setVisibility(View.VISIBLE);
于 2016-12-19T12:28:28.157 回答
0

请参阅此Android 进度条示例

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("In Progress ...");
            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-04-23T06:29:20.470 回答
0

另一个选项是使用漂亮的Spezi-Views,它包含一个非常易于使用的 ProgressButton:

<de.halfreal.spezi.views.ProgressButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press me"
        app:selectedText="I am loaded"
        app:unselectedText="Press me again"
        app:loadingDrawable="@drawable/spinner"
        />

在代码中:

...
//show a rotation spinner, and no text (or the loading text)
progressButton.enableLoadingState();

//show no animation, but the selected/ unselected text
progressButton.disableLoadingState();
...
于 2013-08-19T22:32:10.090 回答