0

我正在做一个假的加载屏幕类型的程序。我希望它显示 1% 然后等待 3 秒然后显示 15% 然后等待 3 秒然后显示 25% 等等。我可以这样设置文本:

prTextView.setText("1%");

我将如何在java中做到这一点?我找到了多个教程,但他们只做了一次而不是多次。

4

3 回答 3

0

您可以使用CountDownTimer. 检查这个https://developer.android.com/reference/android/os/CountDownTimer#java

new CountDownTimer(10000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText(100 - (millisUntilFinished / 1000) + "%");
     }

     public void onFinish() {
         mTextField.setText("100%");
     }
 }.start();
于 2021-08-04T18:39:42.007 回答
0

您可以使用递归(一个调用自身的函数)来循环某些功能。使用 Handler 类等待。

void updateProgress(){
        progress++;
        Handler(Looper.getMainLooper()).postDelayed({
            if(progress < 100)
                updateProgress();
        },3000L);
    }

于 2021-08-04T18:38:23.480 回答
0

检查这个Runnable thread

    private void fakeProgress() {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {

        public void run() {
            final String[] status = {"1%","15%", "25%", "35%", "49%", "60%", "79%", "100%"};
            int arraySize = status.length;
            for (int i = 0; i < arraySize; i++) {
                Log.d("ZI", status[i]);
                String status_part = status[i];
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    public void run() {
                        ((TextView) findViewById(R.id.tv)).setText("........" + status_part);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();
}
于 2021-08-04T20:59:11.970 回答