简体   繁体   English

在Java中停止执行一段时间

[英]Stop the execution for a period of time in java

Hello i am making a quiz game and i have a problem i will try to discribe in a simplier way below.I have a question and 4 buttons each one represents an answer.When i click an answer i want the chosen button to change color to orange for 2 seconds and then goes green if the answer is correct and red if the answer is wrong and the correct answer going green too and stays to this situation for another 2 seconds before bring the new question and all the buttons take their previous(defaut) color. 您好我正在做一个问答游戏,我有一个问题我将尝试以一种更简单的方式在下面进行描述。我有一个问题和4个按钮,每个按钮代表一个答案。当我单击一个答案时,我希望所选按钮将颜色更改为橙色2秒钟,如果答案正确则变为绿色,如果答案错误则变为红色,正确答案也变为绿色,并在这种情况下再停留2秒钟,然后再提出新问题,所有按钮均采用先前的按钮(默认) )颜色。

I have tried many ways like TimeUnit.SECONDS.sleep(2); 我已经尝试了许多方法,例如TimeUnit.SECONDS.sleep(2); , Thread.sleep(2000); Thread.sleep(2000); and some more but the changes appears after that period of time.When i need to appears now and for the next 2 seconds. 还有更多,但更改将在该时间段后出现。当我需要立即显示以及接下来的2秒钟时。

Last way i tried is a timer like this : 我尝试的最后一种方法是这样的计时器:

     long startTime = System.currentTimeMillis();
    long endTime = startTime + 2*1000; // 2 seconds * 1000 ms/sec
    while (System.currentTimeMillis() < endTime)
    {

    }

to stop the execution into the while loop for 2 seconds. 将执行停止到while循环中2秒钟。 I made a simplier application with one button to describe the problem in the forum.I want when i click the button the button's color change to red for 2 seconds and then took the previous color again. 我用一个按钮制作了一个简单的应用程序来描述论坛中的问题。我想当我单击按钮时,按钮的颜色变为红色2秒钟,然后再次使用以前的颜色。 Here is the java code : 这是Java代码:

package com.example.sakis.fillbutton;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    but=(Button)findViewById(R.id.button);
    but.setBackgroundResource(R.drawable.colorbut);


}




public void ClickButton(View view )  {




    but.setBackgroundResource(R.drawable.initcolor);

    long startTime = System.currentTimeMillis();
    long endTime = startTime + 2*1000; // 2 seconds * 1000 ms/sec
    while (System.currentTimeMillis() < endTime)
    {

    }


   but.setBackgroundResource(R.drawable.colorbut);

}

} }

Here is the xml code: 这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sakis.fillbutton.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="88dp"
    android:onClick="ClickButton"
    style="@style/ButtonAnswer"/>

Please help. 请帮忙。

while (System.currentTimeMillis() < endTime) {}

Busy loops are THE BAD THING . 繁忙的循环是最糟糕的事情 Never do that. 绝对不要那样做。 Wiki quote : Wiki报价

In general [...] spinning is considered an anti-pattern and should be avoided, as processor time that could be used to execute a different task is instead wasted on useless activity . daccess-ods.un.org daccess-ods.un.org通常,旋转被认为是一种反模式,应该避免, 因为可以将用于执行其他任务的处理器时间浪费在无用的活动上

Back to the question: 回到问题:

When i click an answer i want the chosen button to change color to orange for 2 seconds and then goes green if the answer is correct and red if the answer 当我单击一个答案时,我希望所选按钮将颜色更改为橙​​色2秒钟,然后如果答案正确则变为绿色,如果答案正确则变为红色

You can use Runnable and post it with required delay, w/o need to worry too much. 您可以使用Runnable并以所需的延迟发布它,而不必担心太多。 When delay pass, your Runnable will be handled. 当延迟通过时,您的Runnable将被处理。 Simply add this to your class members: 只需将此添加到您的班级成员:

Handler mHandler = new Handler();

and then create and post said runnable with handling delayed as much as needed: 然后创建并发布上述可运行的内容,并根据需要延迟处理:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        changesColorsToGreenOrRed();
    }
};
mHandler.postDelayed(runnable, 2000);

and stays to this situation for another 2 seconds before bring the new question 并在提出新问题之前再停留2秒钟

The changesColorsToGreenOrRed() should do its job and then simply post another Runnable . changesColorsToGreenOrRed()应该完成其工作,然后只需发布另一个Runnable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM