[英]How to use randomization, so when you press a button it brings you to a random screen
I have searched for an answer to this question with my time in my Computer Science lab. 我在计算机科学实验室的时间里一直在寻找有关此问题的答案。 We are using Android Studio for this app.
我们正在为此应用程序使用Android Studio。
What I want to do is to use randomization to make a set of screens be randomized when you click a button. 我想做的是,当您单击按钮时,使用随机化使一组屏幕随机化。 My duo is working on a dice rolling app, and we had the idea to make six different screens for each of the sides of the die.
我的二人组正在开发骰子滚动应用程序,我们的想法是为骰子的每个侧面制作六个不同的屏幕。 Basically, when we click the button to "roll the dice", it think for a second, then brings you to a random page with a picture of the number on the die which you got.
基本上,当我们单击按钮“掷骰子”时,它会思考一秒钟,然后将您带到随机页面,其中包含您获得的骰子上的数字图片。
This is incredibly weird, and I have searched for at least 3 hours straight for a solution to this problem but to no avail. 这太不可思议了,我已经连续搜索了至少3个小时,但没有找到解决该问题的方法。 If anybody needs more information on the problem (because I do not know how to properly phrase it), then just ask me.
如果有人需要有关该问题的更多信息(因为我不知道如何恰当地表达它),请问我。
Just use Random.nextInt() to get a random number up to 6, and use that to choose one image of the 6 for each die side. 只需使用Random.nextInt()即可获得一个最大为6的随机数,并使用该数为每个骰子侧选择6的一个图像。 You do not need to create 6 different screens, you just need 6 different images where the number indicates which image to use.
您不需要创建6个不同的屏幕,只需要6个不同的图像,其中的数字表示要使用的图像。 For example:
例如:
// A list of drawables you've defined in /res/drawable folder for each die side
final int[] images = new int[6] {
R.drawable.die_side_1,
R.drawable.die_side_2,
R.drawable.die_side_3,
R.drawable.die_side_4,
R.drawable.die_side_5,
R.drawable.die_side_6
};
int random = Random.nextInt(6); // Get random value, 0-5
int dieSideDrawable = images[random]; // Pick image to show based on random value
mDieImageView.setImageResource(dieSideDrawable); // Show image on an image view
Hope that helps! 希望有帮助!
The easiest way to do exactly what you want would be to put the Activities in an Array, and select by using the Random
class' nextInt
method to choose the appropriate activity from the Array. 精确执行所需操作的最简单方法是将“活动”放入数组中,然后使用“
Random
类的nextInt
方法进行选择,以从数组中选择适当的活动。
That being said, most likely you want to create a single activity with two images, and instead of selecting an activity or Fragment to show, you'd select the images you'd load into the Activity. 话虽这么说,您很可能想用两个图像创建一个活动,而不是选择要显示的活动或片段,而是要选择要加载到活动中的图像。
I would recommend using Fragments to achieve this. 我建议使用片段来实现这一目标。
create a list of fragments 创建片段列表
ArrayList<Fragment> fragmentList = new ArrayList<>();
Now use the java Random class to generate the random number. 现在,使用java Random类生成随机数。
Random rand = new Random();
int n = rand.nextInt(fragmentList.size());
then just show that fragment. 然后显示该片段。
getSupportFragmentManager()
.beginTransaction()
.replace(containerViewId, fragmentList.get(n))
.addToBackStack(null)
.commit();
Using multiple activities seems unnecessary here (and will significantly slow down your app). 在这里似乎不需要使用多个活动(这会大大降低您的应用程序速度)。 If you want to show a different image based on the result of a random number that's been generated, then just .setImageResource() for your Image View based on the result of that random number.
如果要基于生成的随机数的结果显示不同的图像,则只需基于该随机数的结果为Image View显示.setImageResource() 。
In the example below I separated the random number generation (the generateRandomInt() method which stores a random integer in the thisRoll variable) and only called it when the changeImageView() method runs onClick. 在下面的示例中,我分离了随机数生成( generateRandomInt()方法在thisRoll变量中存储随机整数),并且仅在changeImageView()方法运行onClick时才调用它。
public void changeImageView(View view){
generateRandomInt();
if (thisRoll == 1) {
mainImage.setImageResource(R.drawable.s1);
} else if (thisRoll == 2) {
mainImage.setImageResource(R.drawable.s2);
} else if (thisRoll == 3) {
mainImage.setImageResource(R.drawable.s3);
} else if (thisRoll == 4) {
mainImage.setImageResource(R.drawable.s4);
} else if (thisRoll == 5) {
mainImage.setImageResource(R.drawable.s5);
} else {
mainImage.setImageResource(R.drawable.s6);
}
Toast.makeText(DiceRollActivity.this, thisRoll + " ...But The House Always Wins!", Toast.LENGTH_SHORT).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.