我寫了一個記憶遊戲應用程序,我有一個函數調用「OnImageButtonClick」,我插入到佈局中的每個imageButton的onClick屬性中。 當你點擊圖像時,它應該改變它的圖像,但真正發生的是隻有在函數結束後圖像纔會真正改變。Android上setImageResource imageButton
public void onImageButtonClick(View v) throws InterruptedException {
int cardIndex = Integer.parseInt(v.getTag().toString());
ImageButton card = (ImageButton)v;
Resources resources = this.getResources();
int imageId = resources.getIdentifier(imageArr[cardIndex-1], "drawable", this.getPackageName());
card.setImageResource(imageId);
if(isFirst) {
pickedCards[0] = card;
}
else {
pickedCards[1] = card;
}
PickCard();
}
public void PickCard() throws InterruptedException{
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
Toast.makeText(this, "Can't play music", Toast.LENGTH_LONG);
}
if(!isFirst) {
if (pickedCards[0].getBackground() == pickedCards[1].getBackground()){
Points++;
}
else {
Thread.sleep(1000);
pickedCards[0].setImageResource(R.drawable.card);
pickedCards[1].setImageResource(R.drawable.card);
}
}
isFirst = !isFirst;
}
問題是PickCard發生在圖像實際更改之前。
聽起來a'like使用任務沉重的記憶。 PickCard()做什麼? – Opiatefuchs
看看我的編輯@Opiatefuchs –
下面是我說的一個例子:http://stackoverflow.com/a/15709457 –