每秒更新TextView中的数字

时间:2021-10-07 17:55:01

I'm writing my first app in which I need to update the number in a text view every second until a button is pressed. Using a handler seems to be impossible because the number is created and stored outside of the handler, but it can't be declared final because it needs to change. Thread.sleepalso seems to make the app hang indefinitely, before some code is executed that is written above it.

我正在编写我的第一个应用程序,我需要每秒更新文本视图中的数字,直到按下按钮。使用处理程序似乎是不可能的,因为数字是在处理程序之外创建和存储的,但它不能被声明为final,因为它需要更改。 Thread.sleepalso似乎使应用程序无限期挂起,然后执行在其上面执行的某些代码。

final TextView countView = (TextView) findViewById(R.id.counter);
        countView.setText("100,000,000,000");

    //set a Calendar object to be 00:00:00:00 on Jan 1, 2015
    final Calendar startTime = Calendar.getInstance();
    startTime.set(2015, 0, 0, 12, 0, 0);
    startTime.set(Calendar.MILLISECOND, 0);

final Button startButton = (Button) findViewById(R.id.startButton);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //set a Calendar to be the time that the button is clicked and find the difference in ms between it and startTime
                Calendar nowTime = Calendar.getInstance();
                double milStart = startTime.getTimeInMillis();
                double milNow = nowTime.getTimeInMillis();
                double diff = (milNow-milStart)/1000;     //difference in seconds between "start" date and current date

                double total = 100000000000L;   //# that needs to be updated
                for(long i = 0L; i < diff; i++) {
                    total += 1.8;
                }
                countView.setText(NumberFormat.getInstance().format(total));

I need to continue to increment total by 1.8 every second or by 1 every 556 milliseconds.

我需要继续每秒增加1.8或每556毫秒增加1。

while(true) {
   countView.setText(NumberFormat.getInstance().format(total));
   total += 1.8;
   try {
      Thread.sleep(1000);
       } catch (InterruptedException e) {}
 } 

Causes the app to hang indefinitely as soon as the button is clicked, so that even the first countView.setText(NumberFormat.getInstance().format(total)); doesn't execute.

单击按钮后,应用程序将无限期挂起,这样即使是第一个countView.setText(NumberFormat.getInstance()。format(total));不执行。

Using a handler doesn't seem possible to me since total can't work if declared final, but creating a variable inside the handler and looping it would cause the value to never change.

使用处理程序对我来说似乎不可能,因为如果声明为final,则total不能工作,但是在处理程序内创建变量并循环它将导致值永远不会改变。

Am I missing an obvious solution? This is my first real endeavor with Java so it's all new to me

我错过了明显的解决方案吗?这是我对Java的第一次真正努力,所以这对我来说都是新手

1 个解决方案

#1


5  

you may use Handler

你可以使用Handler

Handler h = new Handler();
final Runnable r = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            count++;
            textView.setText(""+count*1.8);
            h.postDelayed(this, 1000); //ms
        }
    };
h.postDelayed(r, 1000); // one second in ms

for stopping you may use h.removeCallbacksAndMessages(null);

停止你可以使用h.removeCallbacksAndMessages(null);

#1


5  

you may use Handler

你可以使用Handler

Handler h = new Handler();
final Runnable r = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            count++;
            textView.setText(""+count*1.8);
            h.postDelayed(this, 1000); //ms
        }
    };
h.postDelayed(r, 1000); // one second in ms

for stopping you may use h.removeCallbacksAndMessages(null);

停止你可以使用h.removeCallbacksAndMessages(null);