Reputation: 1
I'm trying to press the first button just one time. This button usually return a random card but I want it just one time. Then the button should be inactive. How can I do that? That's my code:
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);
}
});
Upvotes: 0
Views: 497
Reputation: 145
If the player has an action to do next, the simpler way is to deactivate the button when is pressed first time and after in the next action activated again.
You can do this adding
background.setEnabled(false);
in your anonymous class listener and after add
background.setEnabled(true);
in player's next action.
Upvotes: 1
Reputation: 66
you can disable your button with (place it inside your onClick)
background.setEnabled(false);
and you also might initialize your button to true in onCreate method
Upvotes: 1