【发布时间】:2014-05-25 19:56:55
【问题描述】:
我试图按一次第一个按钮。这个按钮通常会返回一张随机卡片,但我只想要一次。然后按钮应该是非活动的。我怎样才能做到这一点? 这是我的代码:
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);你试过这样的事情吗?
-
是的,我试过了。它让我可以按一次按钮,但我只需要按两次
-
保留一个统计点击次数的静态计数器。如果 timesClicked > 1 -> setEnabled(false);
-
试试,也有一个循环,但没有任何改变
-
你为什么会有一个循环?这没有什么意义。你是怎么实现的?