2013-03-02 62 views
0

我只有一天的eclipse和java语言的使用经验,今天我已经有很多人回答了这个问题,并成功开发了一个应用程序,就像我希望的那样:)。谢谢大家!Random Picture Shorten

现在我需要的是,我有这样的代码在这里:

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

    final int[] photos={R.drawable.img1, R.drawable.img2,R.drawable.img3,R.drawable.img4}; 

    final ImageView image=(ImageView)findViewById(R.id.imageview1); 

    final Random ran=new Random(); 
    int i=ran.nextInt(photos.length); 
    image.setImageResource(photos[i]); 
    image.setOnClickListener(new View.OnClickListener() 
    { 

而且在这段代码只使用IMG1-IMG4,但我有我的文件夹中的25张图片,但应用程序只显示这4个。我可以在代码中添加所有25个,后来我将有超过100个图像,然后我可以将它们全部添加到代码中,但有什么方法可以缩短代码吗?我会一直将它们命名为img1 ... img25 ... img62 ... img99等等,我应该使用哪些代码来识别和使用文件夹中的所有图像,而不仅仅是写入的图像?

回答

0

使用for循环,非常容易。

for(int x = 0;x<numberofimages;x++) { 
     R.drawable.img + x; //put your code here, the thing I put is wrong. 
    } 

我知道,里面的文字for循环是错误的,但你可以明白这一点,我认为:)