![](/img/trans.png)
[英]Stop receiving all ActionEvents/Stop Listening in Java for a period of time?
[英]Stop the execution for a period of time in java
您好我正在做一個問答游戲,我有一個問題我將嘗試以一種更簡單的方式在下面進行描述。我有一個問題和4個按鈕,每個按鈕代表一個答案。當我單擊一個答案時,我希望所選按鈕將顏色更改為橙色2秒鍾,如果答案正確則變為綠色,如果答案錯誤則變為紅色,正確答案也變為綠色,並在這種情況下再停留2秒鍾,然后再提出新問題,所有按鈕均采用先前的按鈕(默認) )顏色。
我已經嘗試了許多方法,例如TimeUnit.SECONDS.sleep(2);
, Thread.sleep(2000);
還有更多,但更改將在該時間段后出現。當我需要立即顯示以及接下來的2秒鍾時。
我嘗試的最后一種方法是這樣的計時器:
long startTime = System.currentTimeMillis();
long endTime = startTime + 2*1000; // 2 seconds * 1000 ms/sec
while (System.currentTimeMillis() < endTime)
{
}
將執行停止到while循環中2秒鍾。 我用一個按鈕制作了一個簡單的應用程序來描述論壇中的問題。我想當我單擊按鈕時,按鈕的顏色變為紅色2秒鍾,然后再次使用以前的顏色。 這是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);
}
}
這是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"/>
請幫忙。
while (System.currentTimeMillis() < endTime) {}
繁忙的循環是最糟糕的事情 。 絕對不要那樣做。 Wiki報價 :
daccess-ods.un.org daccess-ods.un.org通常,旋轉被認為是一種反模式,應該避免, 因為可以將用於執行其他任務的處理器時間浪費在無用的活動上 。
回到問題:
當我單擊一個答案時,我希望所選按鈕將顏色更改為橙色2秒鍾,然后如果答案正確則變為綠色,如果答案正確則變為紅色
您可以使用Runnable
並以所需的延遲發布它,而不必擔心太多。 當延遲通過時,您的Runnable
將被處理。 只需將此添加到您的班級成員:
Handler mHandler = new Handler();
然后創建並發布上述可運行的內容,並根據需要延遲處理:
Runnable runnable = new Runnable() {
@Override
public void run() {
changesColorsToGreenOrRed();
}
};
mHandler.postDelayed(runnable, 2000);
並在提出新問題之前再停留2秒鍾
changesColorsToGreenOrRed()
應該完成其工作,然后只需發布另一個Runnable
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.