简体   繁体   English

为什么不在Android Studio中显示progressBar?

[英]Why not showing progressBar in Android Studio?

I was trying to work with Progressbar in android studio. 我试图在android studio中使用Progressbar。 What I wanted was simply that when I will click on the button of the MainActivity it will intent to a new activity called 'Progress'. 我想要的只是当我点击MainActivity的按钮时,它将意图进行一项名为'Progress'的新活动。 My activities are here: 我的活动在这里:

MainActivity.java : MainActivity.java

MainActivity.java

and Progress.java : Progress.java

Progress.java

My app runs, but not showing any progressbar motion. 我的应用运行,但没有显示任何进度条动作。 What should I do to show progressbar for 30 sec? 我该怎么办才能显示进度条30秒? I am not doing anything in the meantime. 在此期间我没有做任何事情。 I just want to see a progressbar for 30 sec. 我只想看一个进度条30秒。

1000 is too small for loop but this code do what you want : 1000对于循环来说太小了,但是这段代码可以做你想要的:

//Set visible progres bar
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);

        //Timer in your case 30 sek
        int timer = 30*1000;

        //create handler
        final Handler handler = new Handler();

        //set handler delayed                
        handler.postDelayed(new Runnable() {
            @Override
            public void run() { 

        //Hidden progress bar
        findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);

        //your toast
        Toast.makeText(this,"your msg",Toast.LENGTH_LONG).show();

        //start new activity
        startActivity(your intent)
          }
        }, timer);
    }

mPrograssStatus is increasing to 100 in very short amount of time. mPrograssStatus在很短的时间内增加到100。 You need to wait, for example try adding this to your code in loop while: 您需要等待,例如尝试在循环中将其添加到您的代码中,同时:

SystemClock.sleep(300);

value in brackets is amount of milliseconds that system will wait, in your case it is 300 to get 30 seconds. 括号中的值是系统将等待的毫秒数,在您的情况下,它是300到30秒。 You also need to import library: 您还需要导入库:

import android.os.SystemClock;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM