gpt4 book ai didi

Android for循环和延迟不同步

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:21 26 4
gpt4 key购买 nike

简短摘要

我有一个 Activity 游戏。用户将点击图像:

First level of game

Second level of game

用户有 5 秒的时间点击并选择正确的选项。 5 秒后,正确的 ImageView 将以不同的方式显示以引起注意,5 秒后,用户将再次看到下一个级别。

用户无需等待 5 秒即可点击,因此点击后应立即进入下一级别。

我正在动态创建 View 和 ImageView 。 ImageView 以字节形式从本地数据库 sqlite 中获取。

我的方法是在 oncreate 方法中创建。我使用 for 或 while 循环。每次迭代都是一个游戏级别。每个级别都有不同的 ImageView 和不同的 ImageView 数。

为了延迟 5 + 5 秒,我不得不使用处理程序、线程、倒计时计时器。每一个都造成了问题。

在使用处理程序时,当用户在 5 秒前单击时,我无法终止当前 session 。

不知道这样对不对

你可以在下面看到我的代码:

详细说明和代码

计时器:

nCreate() {
for (int i = 0; i < 2; i++) {
handler.postDelayed(new Runnable() {
public void run() {

imageAnswer.setImageBitmap(bmp);

imageAnswer.setId(R.id.imgAnswer);
//adding image to screen
finalRLayout.removeAllViews();
finalRLayout.addView(imageAnswer, rLayParams);

//starting timer, if no answer in 5 seconds, run again timer
startTimer();

//clicklisterner
imageAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if clicks true, cancel timer and go to next iteration of for loop
if (view == findViewById(R.id.imgAnswer)) {
Log.d(TAG, "clicked");
//PUT IN CODE HERE TO GET NEXT IMAGE
cancelTimer();

} else { //if not true, run again timer for last chance
Log.d(TAG, "nonclick");
cancelTimer();
startTimer();

}
}
});
}
}, 2000 * i);


} //for end

} //oncreate end

//start timer function
void startTimer() {
cTimer = new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
Log.d(TAG, "ontick");
}
public void onFinish() {
Log.d(TAG, "timer onfinish");
}
};
cTimer.start();
}


//cancel timer
void cancelTimer() {
Log.d(TAG, "canceltimer");
if (cTimer != null)
cTimer.cancel();
}

3 个不同的线程操作但没有工作。其中一个使屏幕进入黑屏。另外两个没有阻止循环的进行。

runnable version






for ( int i = 0; i< 20 ; i++) {
Log.d(TAG, "for i2="+ i);
final int finalI = i;
final RelativeLayout finalRLayout = rLayout;



final Runnable r=new Runnable() {

@Override
public void run() {
Log.d(TAG, "for finali2="+ finalI);
TrainingObject trainingObject = new TrainingObject();
trainingObject = trainingObjectList.get(finalI);
objectCount = 2;
//test icin
Log.d(TAG,"testicin trainingobjectid: "+trainingObject.getTrainingobjectID());
object = dbHandler.getObjectObject(trainingObject.getTrainingobjectAnswer());




RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(140,140);

rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);

imgBytes = object.getObjectImageBlob();
bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

imageAnswer.setImageBitmap(bmp);
imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
imageAnswer.setId(R.id.imgAnswer);
finalRLayout.removeAllViews();
finalRLayout.addView(imageAnswer,rLayParams);

}
};
Log.d(TAG, "3000i2 sonrasi");

handler.postDelayed(r, 6000 * i);
imageAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view == findViewById(R.id.imgAnswer)) {
Log.d(TAG, "clicked");handler.removeCallbacks(r); handler.postDelayed(r, 0);
//PUT IN CODE HERE TO GET NEXT IMAGE

}
else{
Log.d(TAG, "nonclick");
handler.removeCallbacks(r); handler.postDelayed(r, 6000);

}
}
});

}//for end

最佳答案

您可以简单地使用处理程序来实现您的目的,您应该做的是创建一个可运行的并在处理程序外部声明它,然后在 postDelayed 处理程序中通过它的名称使用它,如果用户点击,只需删除所有处理程序回调:

// first create runnable
Runnable r = new Runnable{
public void run(){
//do what you need to do after 5 seconds
}
}

// then create handler
Handler mHandler = new Handler();

// then in your onCreate method
nCreate() {
for (int i = 0; i < 2; i++) {
handler.postDelayed(new Runnable() {
public void run() {

imageAnswer.setImageBitmap(bmp);

imageAnswer.setId(R.id.imgAnswer);
//adding image to screen
finalRLayout.removeAllViews();
finalRLayout.addView(imageAnswer, rLayParams);

//starting handler, if no answer in 5 seconds, run again timer
mHandler.postDelayed(r, 5000);

//clicklisterner
imageAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if clicks true, cancel timer and go to next iteration of for loop
if (view == findViewById(R.id.imgAnswer)) {
Log.d(TAG, "clicked");
//PUT IN CODE HERE TO GET NEXT IMAGE
myHandler.removeCallbacks(r);

} else { //if not true, run again timer for last chance
Log.d(TAG, "nonclick");
// stop the handler
myHandler.removeCallbacks(r);
// run a new one
mHandler.postDelayed(r, 5000);

}
}
});
}
}, 2000 * i);


} //for end

} //oncreate end

关于Android for循环和延迟不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com