java - 只需按一次按钮

标签 java android eclipse

我正在尝试按第一个按钮一次。这个按钮通常会返回一张随机卡,但我只想要一次。那么该按钮应该处于非 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/

相关文章:

java - 更改 Java 方法中传递的参数值?

android - 我想制作音乐应用程序(录制语音播放钢琴或吉他)

java - 无法从 Mysql 列出我的 Android 应用程序中的数据

java - Eclipse:继承另一个项目上的Java类

eclipse - 如何设置 Eclipse 中编译的系统属性?

java - com.sun.imageio.plugins.jpeg.JPEGImageReader 泄漏

Java 数组列表帮助

java - Java中如何存储不同行数的数据?

java - 是什么导致我的输出如此困惑?

java - 如何在java中正确指定一个列表