我有3个活动,例如activity_1,activity_2,activity_3。我可以生成1到3之间的随机数。
如何使用此随机数打开相关活动?
答案 0 :(得分:0)
使用if else
条件
//Initialize globally
Random ran = new Random();
int randomNum = ran.nextInt(4 - 1) + 1;
//This gives a random integer between 1 (inclusive) and 4 (exclusive), one of 1,2 and 3.
if(randomNum == 1) {
Intent i = new Intent(getApplicationContext(), ActivityOne.class);
startActivity(i);
}else if(randomNum == 2) {
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
}