我目前正在制作我的第一个Android应用程序,我需要一些帮助。我正在制作的应用程序是记忆游戏Simon。
所以我有两个问题:
1)当用户按下其中一个颜色按钮时,按钮的背景图像切换为发光版本。但是,我怎样才能让按钮在一小段延迟后再次获得原始背景图像?我尝试使用计时器任务(仅适用于红色按钮),但是当我尝试使用它时应用程序崩溃。
2)当计算机通过颜色组合时,这也应该自动发生。有没有办法把它放在一个可以调用的单独函数中?
这是代码: 包android.Simon;
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.TextView; import java.util.Random; import java.util.Timer; import java.util.TimerTask;
公共类Simon扩展了Activity { 天文台m计时器; int [] ComArray = new int [100]; int gebruikergetal; int i = 0; int j = 0; int aantalcomgetallen = 1; //变量以确定计算机必须显示的颜色数量 int gebruikerteller; //变量以确定是否是用户的回合 int startstop = 1; int delay = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button;
final TextView textView = (TextView) findViewById(R.id.textView1);
final Button button1;
final Button button2;
final Button button3;
final Button button4;
for(i=0;i<100;i++){
for(j=0;j<4;j++){
ComArray[i] = RandomCreator.getRandomInt(1, 4);
}
}
i=0;
mChronometer = (Chronometer) findViewById(R.id.chronometer1);
button = (Button) findViewById(R.id.button5);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
final View.OnClickListener Blauw = new OnClickListener() {
public void onClick(View v){
//int GebruikerArray[] = new int[i];
gebruikergetal = 2;
button1.setBackgroundDrawable(getResources().getDrawable(0x7f020001));
//here i'm switching the background image of the button
textView.setText(String.valueOf(gebruikergetal));
if(gebruikergetal!=ComArray[i]){
textView.setText("Game Over");
}
i++;
}
};
final View.OnClickListener Groen = new OnClickListener() {
public void onClick(View v){
//int GebruikerArray[] = new int[i];
gebruikergetal = 3;
button2.setBackgroundDrawable(getResources().getDrawable(0x7f020003));
textView.setText(String.valueOf(gebruikergetal));
if(gebruikergetal!=ComArray[i]){
textView.setText("Game Over");
}
i++;
}
};
final View.OnClickListener Rood = new OnClickListener() {
public void onClick(View v){
//int GebruikerArray[] = new int[i];
Timer timer = new Timer();
gebruikergetal = 1;
button3.setBackgroundDrawable(getResources().getDrawable(0x7f020006));
//this is where I try out the timer task, but the app crashes
TimerTask task = new TimerTask(){
public void run(){
button3.setBackgroundDrawable(getResources().getDrawable(0x7f020005));
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
textView.setText(String.valueOf(gebruikergetal));
if(gebruikergetal!=ComArray[i]){
textView.setText("Game Over");
}
i++;
}
};
final View.OnClickListener Geel = new OnClickListener() {
public void onClick(View v){
//int GebruikerArray[] = new int[i];
gebruikergetal = 4;
button4.setBackgroundDrawable(getResources().getDrawable(0x7f020009));
textView.setText(String.valueOf(gebruikergetal));
if(gebruikergetal!=ComArray[i]){
textView.setText("Game Over");
}
i++;
}
};
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
if(startstop == 1){
mChronometer.start();
textView.setText("Com Turn!");
button1.setOnClickListener(Blauw);
button2.setOnClickListener(Groen);
button3.setOnClickListener(Rood);
button4.setOnClickListener(Geel);
startstop = 0;
}
else{
startstop = 1;
mChronometer.stop();
textView.setText("");
}
}
};
button.setOnClickListener(mStartListener);
}
}
如果有人能提供帮助,我真的很感激。 THX
答案 0 :(得分:1)
抛出你在方法中必须做的事情,并使用所谓的处理程序和runnable。
private Handler mHandler;
mHandler = new Handler();
mHandler.postDelayed(switchImages, delay (in milliseconds);
private Runnable switchImages = new Runnable() {
public void run() {
//Whatever method you make call here
mHandler.postDelayed(switchImages, delay);
//This will call it again therefore continuous updating;
}
};