android中的进度条onclick按钮?

时间:2013-04-23 06:25:20

标签: android location

如何在活动中点击按钮时带上进度条我在主要活动中有一个按钮,如果我点击按钮我想要显示进度条我不想使用辅助活动为此我已完成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();
    }
}

5 个答案:

答案 0 :(得分:7)

你可以这样做

ProgressDialog progress;

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

将此代码称为要显示ProgressBar

的代码

答案 1 :(得分:4)

还有一种方法可以在Button Click上显示ProgressBar。

将此代码放入xml

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

只需在java文件中点击Button即可编写以下代码。

progressBar.setVisibility(View.VISIBLE);

它会在按钮上显示进度条。

答案 2 :(得分: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);

答案 3 :(得分:0)

请参阅此Android Progress Bar Example

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;

    }

}

答案 4 :(得分: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();
...