0
我試圖按一次第一個按鈕。這個按鈕通常會返回一張隨機卡,但我只需要一次。然後該按鈕應該是無效的。我怎樣才能做到這一點? 這是我的代碼:只按一次按鈕
public class GameActivity extends Activity {
Boolean buttonPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final Button background = (Button) findViewById(R.id.A);
Resources res = getResources();
final TypedArray Deck = res.obtainTypedArray(R.array.Deck);
final Random random = new Random();
if (!buttonPressed){
buttonPressed = true;
background.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Genrate a random index in the range
int randomInt = random.nextInt(Deck.length()-1);
// Generate the drawableID from the randomInt
int drawableID = Deck.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
:background.setEnabled(假); 你有沒有試過類似的東西? – Stultuske
是的,我試過了。它給了我一次按下按鈕的可能性,但是我只需要按下它兩次 – user3671673
保留一個靜態計數器來計算點擊次數。如果timesClicked> 1 - > setEnabled(false); – Stultuske