How to create a count-up effect for a textView in Android

后端 未结 6 1498
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 07:17

I am working on an app that counts the number of questions marks in a few paragraphs of text.

After the scanning is done (which takes no time at all) I would love to

6条回答
  •  醉梦人生
    2020-12-13 08:08

    Try this:

    private int counter = 0;
    private int total = 30; // the total number
    //...
    //when you want to start the counting start the thread bellow.
        new Thread(new Runnable() {
    
                    public void run() {
                        while (counter < total) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            t.post(new Runnable() {
    
                                public void run() {
                                    t.setText("" + counter);
    
                                }
    
                            });
                            counter++;
                        }
    
                    }
    
                }).start();
    

提交回复
热议问题