我正在尝试在我的应用程序上使用一个线程来延迟该线程而不卡住 Activity 。问题是即使我在线程上有 Thread.sleep() Activity 也会被卡住。我想做的是一个像西蒙说的游戏,我想在不卡住 Activity 的情况下突出显示一个按钮 2 秒钟,但它被卡住了。该线程称为 NuevoColor,我在 onCreate 方法的末尾启动它。我将代码粘贴在这里,在此先感谢。
package com.example.simondice;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
public class Jugar extends Activity {
public Button boton1Rojo;
public Button boton2Azul;
public Button boton3Verde;
public Button boton4Amarillo;
public ArrayList<Integer> arrayJuego; //se guarda la secuencia del juego
public ArrayList<Integer> arrayJugador; //se guarda la secuencia introducida por el jugador
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jugar);
boton1Rojo = (Button) findViewById(R.id.button1);
boton2Azul = (Button) findViewById(R.id.button2);
boton3Verde = (Button) findViewById(R.id.button3);
boton4Amarillo = (Button) findViewById(R.id.button4);
//cambiamos el color de los botones
boton1Rojo.setBackgroundColor(Color.rgb(127, 0, 0));
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 127));
boton3Verde.setBackgroundColor(Color.rgb(0, 127, 0));
boton4Amarillo.setBackgroundColor(Color.rgb(127, 127, 0));
//botones a disable al iniciar
boton1Rojo.setEnabled(false);
boton2Azul.setEnabled(false);
boton3Verde.setEnabled(false);
boton4Amarillo.setEnabled(false);
//iniciamos el juego
NuevoColor juego = new NuevoColor();
juego.start();
}
// Crea un nuevo color para el juego
public class NuevoColor extends Thread {
@Override public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
arrayJuego = new ArrayList<Integer>();
try {
int rand = 0;
rand = (int) Math.floor(Math.random()*4+1);
if(rand==1) { //iluminamos los botones y los añadimos al array
boton1Rojo.setBackgroundColor(Color.rgb(255, 0, 0));
arrayJuego.add(1);
Thread.sleep(2000);
boton1Rojo.setBackgroundColor(Color.rgb(127, 0, 0));
} else if(rand==2) {
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 255));
arrayJuego.add(2);
Thread.sleep(2000);
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 127));
} else if(rand==3) {
boton3Verde.setBackgroundColor(Color.rgb(0, 255, 0));
arrayJuego.add(3);
Thread.sleep(2000);
boton3Verde.setBackgroundColor(Color.rgb(0, 127, 0));
} else {
boton4Amarillo.setBackgroundColor(Color.rgb(255, 255, 0));
arrayJuego.add(4);
Thread.sleep(2000);
boton4Amarillo.setBackgroundColor(Color.rgb(127, 127, 0));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
}
编辑:感谢大家的回答,现在我更清楚 runOnUiThread for() 是什么。这是经过一些更改后的类(class),以防万一有人发现它有用:
public class NuevoColor extends Thread {
@Override public void run() {
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
cambiarColor();
}
});
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
colorOriginal();
}
});
}
}
最佳答案
只需移动“Thread.sleep(2000);”到您在 UI 线程上运行的 Runnable 之外。我看到您无论如何都想使线程休眠。所以是这样的:
public class NuevoColor extends Thread {
@Override public void run() {
Thread.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// ...
}
});
}
}
您在 NuevoColor 的 run() 中运行的任何内容都是后台工作,并且应该在那里休眠,因为您希望暂停后台线程,而不是整个使用界面。
当然,您抛出给 runOnUiThread() 的 Runnable 中的任何内容都将在 UI 线程上运行,因此它将卡住整个用户界面,直到完成。所以那里没有繁重的工作,也没有 sleep 。
一般来说,可以这样想:任何你想做的事情,都在后台线程中做。任何更新 UI 的东西(如 setBackgroundColor() 等)都在 UI 线程上运行。在您的情况下,只需按照我的指示移动 Thread.sleep(2000) 即可。
关于android - 线程正在卡住 Android 应用程序上的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827106/