如何创建于Android的一个TextView计数时生效 [英] How to create a count-up effect for a textView in Android

查看:106
本文介绍了如何创建于Android的一个TextView计数时生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,计算问号的文本的几个段落的数量。

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

在扫描完成(这需要没有时间),我很想有总额presented后的数字从0到总和。因此,对于10:0,1,2,3,4,5,6,7,8,9的 10 的,然后停止

After the scanning is done (which takes no time at all) I would love to have the total presented after the number goes from 0 to TOTAL. So, for 10: 0,1,2,3,4,5,6,7,8,9 10 and then STOP.

我已经尝试了几种不同的技术:

I have tried a couple of different techniques:

                TextView sentScore = (TextView) findViewById(R.id.sentScore);

                long freezeTime = SystemClock.uptimeMillis();

                for (int i = 0; i < sent; i++) {
                    if ((SystemClock.uptimeMillis() - freezeTime) > 500) {
                        sentScore.setText(sent.toString());
                    }
                }

我也试过这样的:

Also I tried this:

    for (int i = 0; i < sent; i++) { 
        // try {
            Thread.sleep(500);

        } catch (InterruptedException ie) {
            sentScore.setText(i.toString()); 
        } 
    }

我相信这些都是完全业余的尝试。任何帮助将是非常-AP preciated。

I am sure these are both completely amateur attempts. Any help would be much-appreciated.

谢谢

理查德

推荐答案

试试这个:

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();

这篇关于如何创建于Android的一个TextView计数时生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆