我试图在我的应用程序上使用线程来延迟该线程而不冻结活动。问题是即使我在线程上具有Thread.sleep(),活动也被冻结。我想做的是一个像Simon所说的游戏,我想在不冻结活动的情况下突出显示按钮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()了。这是该类在进行一些更改后工作的方法,以防万一有人发现它有用:
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线程上运行,因此它将冻结整个用户界面,直到完成。因此那里没有繁重的工作,也没有睡觉。
通常,这样想:您想做的任何事情,都在后台线程中做。任何更新UI的内容(例如setBackgroundColor()等),都应在UI线程上运行。在您的情况下,按照我的指示移动Thread.sleep(2000)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句