有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何编写带有预设时间的自定义时间进度条?

我想写一段代码,预测一个ProgressBar持续30秒

例如,我们有一个计时器,允许用户决定是否按下此按钮30秒,同时,我们希望用户显示状态栏30秒

我是用Runnable还是For loop来做这件事的


共 (3) 个答案

  1. # 1 楼答案

    为了显示30秒的进度,你可以这样做

    在你的活动/片段中

     public class MainActivity extends AppCompatActivity implements TimerTask{
    
        Timer timer;
        ProgressDialog progressBar;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
            timer = new Timer();
            progressBar = new ProgressDialog(this);  
            progressBar.setCancelable(true);//you can cancel it by pressing back button  
            progressBar.setMessage("Something downloading ...");  
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
            progressBar.setProgress(0); 
            progressBar.setMax(100);
            progressBar.show(); 
            timer.schedule(this,2000);
         }
    
     @Override
        public void run() {
            //here dismiss the progress dialog
            progressBar.dismiss();
        }
    
     }
    
  2. # 2 楼答案

    试试这个:

    private void setProgressDialogAnimation(ProgressBar mProgressBar, int process) {
        ObjectAnimator animation = ObjectAnimator.ofInt(mProgressBar, "progress", 0, process);
        animation.setDuration(30000);//30 second
        animation.setInterpolator(new DecelerateInterpolator());
        animation.start();
    }
    
  3. # 3 楼答案

    我上了这门课,我的问题就解决了

    public class RequestActivity extends AppCompatActivity {
        private ProgressBar progress_bar;
        private TextView    progress_count;
        private int         REQUEST_TIMER  = 0;
        private Timer       timer          = new Timer(); 
        private Handler     handler        = new Handler();
        private int         counter        = 0; 
        private timerTask   task           = new timerTask();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_travel_request);
            progress_bar   = (ProgressBar) findViewById(R.id.progress_bar);
            progress_count = (TextView)    findViewById(R.id.progress_count);
            REQUEST_TIMER  = getIntent().getIntExtra("timeout", 0);
            progress_bar.setMax(REQUEST_TIMER);
            timer.scheduleAtFixedRate(task, 500, 1000);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            task.cancel();
        }
    
        private class timerTask extends TimerTask {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        counter++;
                        progress_count.setText(
                            String.format(Locale.getDefault(), "%d", counter));
                        progress_bar.setProgress(counter);
                        if (counter >= REQUEST_TIMER) {
                            TravelRequestActivity.this.finish();
                            Toast.makeText(
                                getApplicationContext(), "STOP", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    }