我正在尝试按第一个按钮一次。这个按钮通常会返回一张随机卡,但我只想要一次。那么该按钮应该处于非 Activity 状态。我怎样才能做到这一点? 这是我的代码:
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);
}
});
最佳答案
您可以禁用按钮(将其放在 onClick 中)
background.setEnabled(false);
您还可以在 onCreate 方法中将按钮初始化为 true
关于java - 只需按一次按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859413/