android中的进度条onclick按钮?

use*_*156 1 android location

如何在活动中单击按钮时带上进度条我在主要活动中有一个按钮,如果我单击按钮我想显示进度条我不想使用辅助活动为此我已完成2个活动但我需要在主要活动中完成它.

谢谢

我的代码:

家庭活动:

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

启动活动:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Ame*_*iah 7

你可以这样做

ProgressDialog progress;

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

将此代码称为您要显示的代码ProgressBar.

  • 我不是在贬低你,而是使用progress.setCancelable(false); 真是个坏主意. (4认同)

Ami*_*wal 6

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

将此代码放在您的 xml 中

 <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />
Run Code Online (Sandbox Code Playgroud)

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

progressBar.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

它将在您的 Button Click 上显示进度条。