簡體   English   中英

如何在Android中隨機選擇按鈕?

[英]How to randomly select buttons in android?

我想知道如何在android中隨機選擇一個按鈕。 例如,有4個按鈕,我希望應用程序從中隨機選擇一個按鈕並對其執行一些操作。 這是我的代碼:

Button start;
ImageButton btn1, btn2, btn3, btn4, btn5;
Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memory);
    start = (Button)findViewById(R.id.button1);
    start.setOnClickListener(this);
    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);

}

ImageButton[] all= {btn1, btn2, btn3, btn4};

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = all[random.nextInt(all.length)];
        btn5.setBackgroundColor(Color.RED);
    }
}

如果我更改為它,它可以完美工作,但是它將只是btn1而不是隨機選擇。

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = btn1;
        btn5.setBackgroundColor(Color.RED);
    }
}

您需要將此行放在onCreate()方法中:

all= {btn1, btn2, btn3, btn4};

現在是這樣:

ImageButton[] all= {btn1, btn2, btn3, btn4};

它在所有按鈕都為空的承包商時間內運行。 與在onCreate中為新變量分配按鈕相比,這不會改變ImageButton。 或者,您可以這樣寫:

ImageButton[0] = (ImageButton)findViewById(R.id.imageButton1);
ImageButton[1] = (ImageButton)findViewById(R.id.imageButton2);
ImageButton[2] = (ImageButton)findViewById(R.id.imageButton3);
ImageButton[3] = (ImageButton)findViewById(R.id.imageButton4);

all已設置必須設置btn1否則這將是一個數組:等null秒。

    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);
    all= {btn1, btn2, btn3, btn4}; //here
}

ImageButton[] all; //not here

如何在Java中生成特定范圍內的隨機整數?

檢查上面的參考以生成隨機數,然后使用

switch(number)
{
case 1:
button.performClick()

.....
}

這樣做

 ImageButton[] all= {btn1, btn2, btn3, btn4};

 Random rand = new Random();
 ImageButton randomBtn = all[rand.nextInt(all.length)];

我目前正在運行以下代碼:

    Button start;
    ImageButton btn1, btn2, btn3, btn4, btn5;
    Random random;

    int[] all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        random = new Random();

        start = (Button)findViewById(R.id.button1);

        start.setOnClickListener(this);

        btn1 = (ImageButton)findViewById(R.id.ImageButton01);
        btn2 = (ImageButton)findViewById(R.id.ImageButton02);
        btn3 = (ImageButton)findViewById(R.id.ImageButton03);
        btn4 = (ImageButton)findViewById(R.id.ImageButton04);

        all = new int[]{R.id.ImageButton01,R.id.ImageButton02,R.id.ImageButton03,R.id.ImageButton04};
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1)
        {
            int id = all[random.nextInt(all.length)];
            findViewById(id).setBackgroundColor(Color.BLACK);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM