通过使用随机阵列更改按钮的背景图片 [英] Changing a buttons background image through using a random array

查看:100
本文介绍了通过使用随机阵列更改按钮的背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,所以我试图让第二次点击此按钮可以显示图像。我有8个图像,它可以选择,我希望它随机选择它。我在字符串中建立一个数组与所有的 R.drawable.img ,我试图把它里面的这个

OK so I'm trying to get this button on its second click to display an image. I have 8 images for it to choose from and I want it to select it randomly. I set up an array with all of the R.drawable.img in the string and i tried placing it inside of this

   else if (click == 1)
                {
                    rpic = generator.nextInt(ppic);
                    spinntoke.setBackgroundResource(R.pic[rpic]);
                }

所以它不允许我这样做。任何想法如何,我可以得到随机发生器随机选择的8张图片的1被点击的时候吗?谢谢

So it is not allowing me to do that. Any ideas as to how i can get the random generator to select 1 of those 8 pictures at random when it is clicked? Thanks

推荐答案

您还没有真正提供足够的信息,但这里是什么我想你会怎么做。

You have not really provided enough info but here is what I imagine you would do.

有一个数组INT [],看起来像这样:[R.drawable.img1,R.drawable.img2,R.drawable.img3]

have an array int[] that looks like this: [R.drawable.img1, R.drawable.img2, R.drawable.img3]

在的onClick():随机=一些随机的0和array.size() - 1之间;
spinntoke.setBackgroundResource(数组[随机]);

in onClick(): random = some random between 0 and array.size()-1; spinntoke.setBackgroundResource(array[random]);

这种方式你的整数(你ř资源)数组,其中你可以选择一个随机。不要忘记,让您的随机数发生器只能生成从0号码数组大小-1。

This way you have an array of ints (your R resources) where you can pick a random one. Don't forget to make your random generator only generate numbers from 0 to the array size-1.

编辑:code:

Random randomGenerator = new Random();
int random = randomGenerator.nextInt(array.size());
spinntoke.setBackgroundResource(array[random]);

这篇关于通过使用随机阵列更改按钮的背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆