开发者

How can I build the following Android activity?

onCreate, MyActivity will display five TextViews. After you touch one of the five TextViews, it will hide three to four TextViews and color one red and one green or just color one green.

I can code everything up to here. But how can I pause for a few seconds and then repopulate the five TextViews with new values, unhide them and make them all white?

Thanks in advance!

NEW EDIT NEW EDIT NEW EDIT

I tried a Timer in a new project and can attach the code and make my question less vague.

Here is the main.xml

<?xml version="1.0" e开发者_高级运维ncoding="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/hello"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" />
</LinearLayout>

Here is the TestTimer.java

package com.somecompany.android.testtimer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TestTimer extends Activity {
    TextView hello;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    hello = (TextView)findViewById(R.id.hello);
    hello.setTextColor(Color.rgb(0,255,0));

    new Reminder(5);
    }

    void resetAndContinue() {
    Log.d("TESTTIMER", "Start resetAndContinue...");
    hello.setTextColor(Color.rgb(255,255,255));
    Log.d("TESTTIMER", "End  resetAndContinue...");
    }

    class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
        Log.d("TESTTIMER", "Ran TagRemindTask");
        resetAndContinue();
        timer.cancel(); //Terminate the timer thread
        }
    }


    }
}

The issue is that the Timer executes resetAndContinue and logs two entries, but it doesn't set the TextView color from green to white and it doesn't log anymore

07-15 13:08:46.894: DEBUG/TESTTIMER(618): Ran TagRemindTask

07-15 13:08:46.894: DEBUG/TESTTIMER(618): Start resetAndContinue...

07-15 13:08:47.264: DEBUG/dalvikvm(524): GC freed 202 objects / 8936 bytes in 156ms

07-15 13:08:52.224: DEBUG/dalvikvm(210): GC freed 43 objects / 2096 bytes in 85ms


Although your question is very general and vague, I'll try to answer it:

To "pause" your Activity, there are several possibilities. If it doesn't have to react on user input, but just execute a specific method after a specific amount of time, I would recommend to use TimerTask in combination with Timer. For an explanation of how this works you can use this reference.

To set new values to a TextView (assuming that new values means a new text to display), you should use the method TextView.setText(...). To hide/unhide a TextView, you could use the method setVisibility(...). To make a TextView white (assuming that you're talking of the TextView background) you can use the method setBackgroundColor(...).

I hope this will help you. If you have any questions, you have to provide us with code and more detailed questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜