Android 处理程序定期

发布于 2024-10-06 05:27:22 字数 308 浏览 0 评论 0 原文

我想要实现这一点:

  1. 一个活动以没有 ClickListener 的方式开始,并且有四个带有白色背景的文本视图

  2. 我想更改textview 1 的颜色为蓝色。等待 2 秒,然后将其改回白色,并将 textview 2 更改为蓝色。等待 2 秒,然后将其更改回白色...依此类推,直到我将 textview 4 更改为蓝色并返回白色。

  3. 完成后,我想添加 ClickListener 并等待用户输入。

我怎样才能实现这个目标?我是 Android 新手,但了解一些细节。

This I want I want to achieve:

  1. An activity starts with no ClickListener and has four textviews all with white background

  2. I want to change the color of textview 1 to blue. Wait for 2 seconds and then change it back to white and change the textview 2 to blue. wait for 2 seconds and then change it back to white... so on till i have turned textview 4 to blue and back to white.

  3. Once that is complete, I want to add the ClickListener and wait for user input.

How can I achieve this? I am new to Android but understands bits and pieces.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2024-10-13 05:27:22

您可以通过创建 动画 序列来实现此目的,无论是XML 或 Java 代码,并按顺序触发它们。您需要使用 LayoutAnimationController 定义动画序列,位于动画结束时,您可以添加ClickListener。

Developer Life 有一个帮助您开始使用动画的好教程。 Jeff 有一个由两部分组成的动画教程系列 - 部分1第 2 部分

希望这有帮助,
印地弗罗兹

You can achieve this by creating Animation sequences, in either XML or Java code, and triggering them in sequence. You will need to define a animation sequence with LayoutAnimationController, at the end of the animation, you can add the ClickListener.

Developer Life has a good tutorial to get you started on animations. Jeff has a two-part tutorial series on animations - part 1, part 2.

Hope this helps,
indyfromoz

云胡 2024-10-13 05:27:22

无需为此创建线程或动画。

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

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

为了实现稳健,请确保至少通过 Activity.onDestroy() 删除任何待处理的消息(或者如果您正在发布消息)。在 Activity.onStart() 中删除它们,在 Activity.onStop() 中删除它们;如果在 Activity.onResume() 中发布,则在 Activity.onPause() 中删除。)

There is no need to create a thread for this, or animations.

The solution is really simple: use Handler.postDelayed() or Handler.sendMessageDelayed():

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

For a robust implementation, be sure to remove any pending messages at least by Activity.onDestroy(). (Or if you are posting them in Activity.onStart(), remove them in Activity.onStop(); if posting in Activity.onResume(), remove in Activity.onPause().)

一身软味 2024-10-13 05:27:22

我有一个用于此任务的示例,但是通过带有handleMessage

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;
    }
}

kayout的线程:

<?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>

祝你好运,

I have one sample for this task but by thread with handleMessage

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;
    }
}

kayout:

<?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>

Good luck,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文