如何在屏幕上显示生成随机数

时间:2013-12-01 17:43:18

标签: android layout random numbers imageview

我正在尝试创建一个会生成随机数的应用。我发现这生成了数字编码,它工作得很好。但是,当我想在布局中显示生成编号时,我遇到了问题。

例如,生成器将生成两个数字; numberOne = 3,numberTwo = 1。然后它将在布局页面上显示imageview编号3和imageview编号1。由于我是新手,所以如何根据生成的数字显示数字的图像视图。谢谢:))

生成数字编码

Random randomNum=new Random();
int numberOne;

numberOne=1+randomNum.nextInt(10);
System.out.println(numberOne + " ");

这是显示的示例

This is the example of the display

1 个答案:

答案 0 :(得分:0)

您只需将图像加载到已定义的ImageView中。

// fetch image resource id
// first parameter is the image name without the extension
int id = getResources().getIdentifier("image_name", "drawable", getPackageName());
// assign it to ImageView
imageView.setImageResource(id);