想要显示1秒延迟的号码

时间:2014-03-16 05:28:26

标签: android multithreading android-activity delay sleep

我想在TextView中显示数字,如下所示:

Wait 5 sec // then a delay of 1 sec
Wait 4 sec // display this in the same text view along with delay
Wait 3 sec // display this in the same text view along with delay
Wait 2 sec // display this in the same text view along with delay
Wait 1 sec // display this in the same text view along with delay

我想在点击按钮时执行此操作,它应该像倒计时器一样工作。

4 个答案:

答案 0 :(得分:1)

如果我理解正确,你只需要在点击按钮时从5秒开始倒数计时器。如果是这样,请试一试:

public class MainActivity extends Activity
{
    TextView textView;
    Button button;

    private final Handler mHandler = new Handler();
    private int startTime = 5;
    private Runnable mTask = new Runnable()
    {
        public void run()
        { 
            if (startTime > 0)
            { 
                textView.setText("Wait for " + String.valueOf(startTime));
                startTime--;
                mHandler.postDelayed(mTask, 1000); 
            } 
            else 
            {
                textView.setText("Sorry. Times up!");
            }
        } 
    };

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

        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mHandler.post(mTask);
                }               
            }
        );      
    }   
}

答案 1 :(得分:1)

你可以使用这个

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("Wait: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

此计时器类可以在按钮点击时启动,也可以调整总时间和刻度间隔

这是来自developer网站

答案 2 :(得分:1)

非常简单,您需要使用CountDownTimer类。

这是一个工作演示,我刚刚为您创建。

MainActivity.java

public class MainActivity extends Activity 
{
    int remaningTime = 5;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById( R.id.txtTextView );
        Button button1 = (Button) findViewById( R.id.button1 );

        button1.setOnClickListener( new OnClickListener ()
        {

            @Override
            public void onClick(View arg0) 
            {
                new CountDownTimer ( remaningTime * 1000, 1000 )
                {

                    @Override
                    public void onFinish() 
                    {
                    }

                    @Override
                    public void onTick(long millisUntilFinished) 
                    {
                        textView.setText( "Wait " + ( remaningTime-- ) + " sec" );
                    }
                }.start();
            }           
        });
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtTextView"
        android:layout_below="@+id/txtTextView"
        android:layout_marginTop="34dp"
        android:text="Button" />

</RelativeLayout>

答案 3 :(得分:0)

使用AsyncTask,以下是一些示例代码:

...

private class CounterTask extends AsyncTask<null, null, null> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Long doInBackground(Uri... uri) {
        try {
            mCount++;
            updateUIHandler.obtainMessage().sendToTarget();
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Long result) {
    }
}

public Handler updateUIHandler = new Handler() {
    public void handleMessage(Message msg) {
        mText.setText(mCount);
    }
};

mText是您的TextView,mCount是您的计数器,这些可以是全局变量。正如iTech所说,您无法从线程更新UI,但是您可以在UI线程上调用Handler来进行更新。

运行这样的任务:

new CounterTask().execute(null, null, null);