2012-02-22 113 views
0

我在Eclipse中使用Android。我有我的隨機圖像發生器工作。在加載頁面的這一刻,在用戶單擊按鈕之前,ImageView1中會自動加載一個隨機圖像。 (在用戶點擊按鈕之後,另一個隨機圖像被加載)。 *當頁面加載時,我不希望任何圖像顯示在ImageView1上,直到用戶單擊按鈕。我怎麼能夠更改我的代碼,以便在用戶單擊按鈕之前不會自動加載圖像?隨機圖像生成器Android

package com.senior.callacab2; 

    import java.util.Random; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.widget.ImageView; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class Generatedrinks extends Activity implements OnClickListener { 


private int[] mPhotoIds = new int [] { R.drawable.img1, 
     R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, 
     R.drawable.img6, R.drawable.img7 }; 

private static final Random rgenerator = new Random(); 

private ImageView iv; 

// Makes new images appear after every button click 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.generatedrinks); 

    Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)]; 
    iv = (ImageView) findViewById(R.id.imageView1); 
    iv.setImageResource(q); 

    View nextButton = findViewById(R.id.button2); 
    nextButton.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.button2: 

iv.setImageResource(mPhotoIds[rgenerator.nextInt(mPhotoIds.length)]); 
     break; 
    } 

    // Makes button2 invisible after two clicks the first click generates a 
    // random image and the second click makes the button disappear 
    Button generator = (Button) findViewById(R.id.button2); 
    generator.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Button button = (Button) v; 
      button.setVisibility(View.INVISIBLE); 
     } 
    }); 
    // When this button is pressed it takes the user to the next layout 
    // called Scoreclass.class 

    Button next = (Button) findViewById(R.id.button1); 
    next.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Scorecard.class); 
      startActivityForResult(myIntent, 0); 
     } 

    }); 

} 

    } 

回答

1

好吧,如果你不想讓你的活動點擊之前,那麼當您啓動活動不加載圖像加載圖像...

你必須在此代碼您onCreate

Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)]; 
iv = (ImageView) findViewById(R.id.imageView1); 
iv.setImageResource(q); 

取下第一和第三行(第二個仍然是有用的發現,供以後使用視圖)和你的活動開始時,你就不會加載圖像。

+0

謝謝這工作完美。 – eg8842 2012-02-27 22:16:52

+0

很高興幫助。如果這回答您的問題令您滿意,則可以單擊此答案旁邊的複選標記以將其標記爲接受的答案。 – kabuko 2012-02-27 22:29:18