我的数组包含的元素多于此处发布的元素。我只显示2个帖子,但是有10个showGreen
变量,10个其他线程,10个“ TextViews”和10个isGreenBackgroundShouldAppear
函数。如您所见,当答案正确时,TextView1
或TextView2
在ACTION_DOWN
上变为绿色,并在image_view
中设置随机图像,从而准备下一个问题。问题是,如果我使用10个线程分别每500ms为每个TextView
设置答案的值,那么如果用户过快按下正确答案TextView
,它将被视为错误答案(红色)。如果我将500ms设置为300ms或更短,有时showGreen
永远不会正确更新。您可能知道我的问题的解决方案吗?
class Cats : AppCompatActivity() {
var cats = intArrayOf(
R.drawable.cat1,
R.drawable.cat2,
R.drawable.cat3,
R.drawable.cat4,
R.drawable.cat5,
R.drawable.cat6,
R.drawable.cat7,
R.drawable.cat8,
R.drawable.cat9,
R.drawable.cat10,
R.drawable.cat11,
R.drawable.cat12,
R.drawable.cat13,
R.drawable.cat14,
R.drawable.cat15,
R.drawable.cat16,
R.drawable.cat17
)
lateinit var random: Random
var showGreen1 = false
var showGreen2 = false
val runnable1 = Runnable {
while (true) {
showGreen1 = isGreenBackgroundShouldAppear1()
Thread.sleep(500)
}
}
val runnable2 = Runnable {
while (true) {
showGreen2 = isGreenBackgroundShouldAppear2()
Thread.sleep(500)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
random = Random()
Thread(runnable1).start()
Thread(runnable2).start()
TextView1.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
if (showGreen1) {
TextView1.setBackgroundResource(R.color.green);
image_view.setImageResource(cats[random.nextInt(cats.size)])
/*showGreen1 = isGreenBackgroundShouldAppear1()
showGreen2 = isGreenBackgroundShouldAppear2()*/
} else {
TextView1.setBackgroundResource(R.color.red);
}
}
MotionEvent.ACTION_UP -> {
TextView1.setBackgroundResource(R.color.white)
}
}
return@OnTouchListener true
})
TextView2.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
if (showGreen2) {
TextView2.setBackgroundResource(R.color.green);
image_view.setImageResource(cats[random.nextInt(cats.size)])
} else {
TextView2.setBackgroundResource(R.color.red);
}
}
MotionEvent.ACTION_UP -> {
TextView2.setBackgroundResource(R.color.white)
}
}
return@OnTouchListener true
})
/*while(true)
{
showGreen1 = isGreenBackgroundShouldAppear1()
showGreen2 = isGreenBackgroundShouldAppear2()
}*/
fun isGreenBackgroundShouldAppear1(): Boolean {
return image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat1
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat2
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat3
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat4
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat5
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat6
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat7
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat8
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat9
)?.constantState
}
fun isGreenBackgroundShouldAppear2(): Boolean {
return image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat10
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat11
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat12
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat13
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat14
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat15
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat16
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat17
)?.constantState
}
}