Android的简单的TextView文本更改计时器计时器、文本、简单、Android

2023-09-04 04:10:44 作者:病娇萝莉が

我是新来的Andr​​oid和通过一些视频教程刚刚开始。我的要求是首先我需要显示的TextView文本为红后5-10秒,需要将其更改为hello红。

I am new to android and just starting through some tutorial videos. My requirement is initially i need to show the textview text to "red" and after 5-10 sec, need to change it to "hello red".

我已经尝试了两种方法交替。在第一种方法它给我的异常,而在第二个方法的启动应用程序给定的延时后,直接告诉我你好红的文字。我可能会在这里缺少一些基本概念。你能请帮助我吗?

I have tried two method alternatively. In first method its giving me exception while in second method its starting the application after given delay and directly show me the "hello red" text. I might be missing some basic concepts here. Could you pls help me ?

       TextView myText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myText = (TextView)findViewById(R.id.displayTv);
    // ------ first method start ---------
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(6000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                myText.setText("Hello red");
            }
        }
    };
    timer.start();
    // ------ first method end ---------
    // ------ second method start ---------
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    myText.setText("Hello red");
    // ------ second method end ---------
}

}

推荐答案

尝试使用处理器,

Handler h=new Handler();
h.postDelayed(new Runnable(){
public void run(){
//change your text here
}
}, time_delay);