首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Handler定期

Android Handler定期
EN

Stack Overflow用户
提问于 2010-12-04 21:26:06
回答 3查看 2.5K关注 0票数 6

这是我想要实现的:

  1. 一个活动从没有ClickListener开始,有四个文本视图,都是白色背景。
  2. 我想将textview 1的颜色更改为蓝色。等待2秒,然后将其更改为白色,并将文本视图2更改为蓝色。等2秒,然后换回白色.直到我把文本视图4变成蓝色和白色。
  3. 一旦完成,我想要添加ClickListener并等待用户输入。

我怎样才能做到这一点?我对Android并不陌生,但我能理解一些零碎。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-04 22:35:54

没有必要为这个或动画创建一个线程。

解决方案非常简单:使用Handler.postDelayed()或Handler.sendMessageDelayed():

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable http://developer.android.com/reference/android/os/Handler.html#sendMessageDelayed(android.os.Message

对于健壮的实现,请确保至少通过Activity.onDestroy()删除任何挂起的消息。(或者如果您在Activity.onStart()中投递,请在Activity.onStop()中删除;如果在Activity.onResume()中投递,则在Activity.onPause()中删除。)

票数 1
EN

Stack Overflow用户

发布于 2010-12-04 21:54:31

您可以通过在XML或Java代码中创建动画序列并按顺序触发它们来实现这一点。您需要用LayoutAnimationController定义动画序列,在动画的末尾,可以添加ClickListener。

Developer有一个很好的教程,让你开始学习动画。杰夫有一个关于动画的两部分教程系列- 第1部分第2部分.

希望这能帮上忙

票数 2
EN

Stack Overflow用户

发布于 2010-12-04 22:13:12

我有一个用于此任务的示例,但使用handleMessage线程。

代码语言:javascript
复制
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;
import android.widget.TextView;

public class l15_threadOneaacto extends Activity  {
    /** Called when the activity is first created. */
    TextView tv[]=new TextView[4];
    EditText et;
    Thread bcko;
    static int index=0;

    boolean isRunning=false;
    boolean acceptevent=false;
    Handler hn=new Handler(){
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (index) {
            case 0: tv[0].setBackgroundColor(Color.BLUE);  break;
            case 1: tv[0].setBackgroundColor(Color.WHITE);  break;
            case 2: tv[1].setBackgroundColor(Color.BLUE);  break;
            case 3: tv[1].setBackgroundColor(Color.WHITE);  break;
            case 4: tv[2].setBackgroundColor(Color.BLUE);  break;
            case 5: tv[2].setBackgroundColor(Color.WHITE);  break;
            case 6: tv[3].setBackgroundColor(Color.BLUE);  break;
            case 7: tv[3].setBackgroundColor(Color.WHITE);  break;
            }
            index++;
            if(index==8){
                acceptevent=true;
                et=(EditText)findViewById(R.id.bbb);
                et.setText("ready for input");
            }

        };

    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv[0]=(TextView)findViewById(R.id.tx1);
        tv[1]=(TextView)findViewById(R.id.tx2);
        tv[2]=(TextView)findViewById(R.id.tx3);
        tv[3]=(TextView)findViewById(R.id.tx4);





    }




    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

         bcko=new Thread (new Runnable() {

            @Override
            public void run() {
                try {

                        //
                        for(int i=0;i<8 && isRunning;i++){
                            Thread.sleep(2000);
                        hn.sendMessage(hn.obtainMessage());
                        }

                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
        });
        isRunning=true;
        bcko.start();



    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        isRunning=false;
    }
}

划独木舟:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/tx1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF" 
    android:text=""
    />
    <TextView  
    android:id="@+id/tx2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF" 
    android:text=""
    />
    <TextView  
    android:id="@+id/tx3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF" 
    android:text=""
    />
    <TextView  
    android:id="@+id/tx4"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF" 
    android:text=""
    />
    <EditText
    android:id="@+id/bbb"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="not ready "
    />
</LinearLayout>

祝好运,

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4355919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档