如何在循环中更新Textview文本

时间:2014-04-14 22:18:51

标签: android while-loop textview

我想要一个非常简单的XML布局,其中只有一个textview。我希望文本视图每秒上升一次。为此,我使用了此代码

    public class test extends Activity{
    TextView num;
    int to =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatexml);
num = (TextView) findViewById(R.id.tvUpdate);
        startIncrementing();
    }
    public void startIncrementing() {
        while (to < 100) {
            num.setText(to + "");
            to++;
        }
        }
    }

已经做了一个很好的搜索时间,但没有找到一个好的答案。任何帮助都会很棒。

编辑:

使用下面给出的答案并在错误日志中得到这个:

04-15 17:35:17.637: E/AndroidRuntime(28985): FATAL EXCEPTION: main
04-15 17:35:17.637: E/AndroidRuntime(28985): Process: com.infitenothing.dogcat, PID: 28985
04-15 17:35:17.637: E/AndroidRuntime(28985): java.lang.NullPointerException
04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.infitenothing.word.Test2$1.run(Test2.java:41)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Handler.handleCallback(Handler.java:733)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Looper.loop(Looper.java:136)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
04-15 17:35:17.637: E/AndroidRuntime(28985):    at dalvik.system.NativeStart.main(Native Method)

当前代码

public class Test2 extends Activity {

    // Init var
    int i=0;
    TextView num;
    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Init your TextView
        num = (TextView) findViewById(R.id.tvUpdate);

        // Timer
        TimerTask myTask = new TimerTask() {
            public void run() {
                updateUI(); // updateUI method
            }
        };
        myTimer.schedule(myTask,0,1000); // TimerTask, delay, period
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            num.setText(String.valueOf(i)); // update your text
        }
    };

    // updateUI method related to a Runnable
    private void updateUI() {
        if(i < 100) {
            i++;
            // num.setText(String.valueOf(i)); = avoid the RunTime error
            myHandler.post(myRunnable); // relate this to a Runnable
        } else {
            myTimer.cancel(); // stop the timer
            return;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您应该创建与Timer相关的Runnable以避免RunTime错误),每秒更新一次视图:

public class MainActivity extends Activity {

    // Init var
    int i=0;
    TextView num;
    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Init your TextView
        num = (TextView) findViewById(R.id.textNum);

        // Timer
        TimerTask myTask = new TimerTask() {
            public void run() {
                updateUI(); // updateUI method
            }
        };
        myTimer.schedule(myTask,0,1000); // TimerTask, delay, period
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            num.setText(String.valueOf(i)); // update your text
        }
    };

    // updateUI method related to a Runnable
    private void updateUI() {
        if(i < 100) {
            i++;
            // num.setText(String.valueOf(i)); = avoid the RunTime error
            myHandler.post(myRunnable); // relate this to a Runnable
        } else {
            myTimer.cancel(); // stop the timer
            return;
        }
    }
}

答案 1 :(得分:0)

尝试使用CountDownTimer。您可以修改代码以使其上升。可以在此处找到示例:http://developer.android.com/reference/android/os/CountDownTimer.html