runOnUiThread,更改TextView

时间:2012-03-17 13:36:56

标签: java android multithreading

我想写一个实时计数器。我试图使用线程,但android警告我只有活动线程可能触摸视图。我找到了runOnUiThread的解决方案,但它也不起作用

    public class CounterInRealTimeExampleActivity extends Activity {
        private TextView textView;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LinearLayout layout = new LinearLayout(this);
            textView = new TextView(this);
            textView.setTag(new Integer(0));

            layout.addView(textView);
            setContentView(layout);
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    //while(true)
                    {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        increment();
                    }
                }
            });
        }
        public void increment() {
            textView.setTag(new Integer((Integer)textView.getTag())+1);
            textView.setText(Integer.toString((Integer)textView.getTag()));
        }
    }

3 个答案:

答案 0 :(得分:4)

我在这里粘贴代码。这可能对你有所帮助..

class UpdateTimeTask extends TimerTask {    

    public void run() {

        String time =  DateUtils.now("hh:mm:ss'T'a");              
        String[]arrValues = time.split("T");
        if(arrValues.length>0) {
            String strValue= arrValues[0];
            String []arrTimeValues = strValue.split(":");  
            String strValue1= arrTimeValues[2];
            setTimertext(strValue1);                   
        }                      
    }

    public void setTimertext(String strValue) {         
        runOnUiThread(new Runnable() {
            public void run() {
                FinalTime=timer_time--;                 
                btnTimer.setText(String.valueOf(FinalTime));    
            }
        });
    }

}

答案 1 :(得分:3)

Thread.sleep(3000);

绝对是个问题。您不能阻止UI线程,这正是sleep所做的。您必须执行代码才能在runOnUiThread 内更新用户界面。

答案 2 :(得分:2)

工作一,在Hasmukh提示之后找到解决方案

package com.android.examples;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CounterInRealTimeExampleActivity extends Activity {
    private TextView textView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        textView = new TextView(this);
        layout.addView(textView);
        setContentView(layout);

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {

                    updateTime();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    private void updateTime() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                textView.setText(Integer.toString((int) (Calendar.getInstance()
                        .getTimeInMillis() / 1000) % 60));
            }
        });
    }
}