2011-03-14 56 views
0

我正在使我的隨機生成圖像(讀作按鈕)變得可點擊,導致每個不同的圖像的每個不同的活動touble。所以隨機圖像完美的工作實際上,它是不可點擊的唯一的問題是我的代碼Android - 設置隨機按鈕圖像可點擊

 final Button imgView = (Button)findViewById(R.id.top1); 
     Random rand = new Random();    
     int rndInt = rand.nextInt(4) + 1; 
     String imgName = "img" + rndInt; 
     int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
     imgView.setBackgroundResource(id); 

在我的佈局,我指定的ID TOP1的按鈕。 因此,上面的代碼將查找我的可繪製圖像,其名稱分別爲'img1.jpg','img2.jpg','img3.jpg'和'img4.jpg'。

所以我想要做的就是當'img1.jpg'生成時,它變得可點擊並導致例如:Activity1.java,'img2.jpg'意圖轉到'Activity2.java'等等。

非常感謝。我打開的任何一種解決方案:)

更新:

這是我的主類的全碼:

public class Main extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main_x); 



      final Button imgView = (Button)findViewById(R.id.top1); 
      Random rand = new Random(); 

      imgView.setOnClickListener(new ActivitySwitch(1,this)); 
      imgView.setOnClickListener(new ActivitySwitch(2,this)); 
      imgView.setOnClickListener(new ActivitySwitch(3,this)); 
      imgView.setOnClickListener(new ActivitySwitch(4,this)); 

      int rndInt = rand.nextInt(4) + 1; 
      String imgName = "img" + rndInt; 
      int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
      imgView.setBackgroundResource(id); 

     } 

這裏的ActivitySwitch類:

public class ActivitySwitch implements OnClickListener{ 
    int imageNo; 
    Context context; 
    public ActivitySwitch(int imageNo,Context context) { 
     super(); 
     this.context=context; 
     this.imageNo = imageNo; 
    } 
    @Override 
    public void onClick(View v) { 
     Intent it=new Intent(); 
     if(imageNo==1) 
     { 
     it.setClass(context, ProjektAID.class); 
     } 
     else if (imageNo==2) 
     { 
     it.setClass(context, ProjektADH.class); 
     } 
     else if (imageNo==3) 
     { 
     it.setClass(context, ProjektBOS.class); 
     } 
     else if (imageNo==4) 
     { 
     it.setClass(context, ProjektBROT.class); 
     } 

    } 

} 
+0

您所添加的OnClickListener到您的ImageView? – RoflcoptrException 2011-03-14 08:58:16

+0

以及我的朋友你也需要開始活動。我在我的答案plz中做了一些更改,請參閱 – 2011-03-14 12:52:56

回答

0

有一種方法:創建一個新類

class ActivitySwitch implements OnClickListener{ 
    int imageNo; 
    Activity context 
    public ActivitySwitch(int imageNo,Context context) { 
     super(); 
     this.context=(Activity)context; 
     this.imageNo = imageNo; 
    } 
    @Override 
    public void onClick(View v) { 
     Intent it=new Intent(); 
     if(imageNo==1){ 
     it.setClass(context, Activity1.class); 
      } 
      else{ 
      ....... 
      } 
      startActivityForResult(it,any_integer_value); 




    } 

} 

然後在Activity集:

imgView.setOnClickListener(new ActivitySwitcher(randInt,this); 
+0

你好,所以我做了像上面提到的ActivityClass,然後在Main類中添加了這個:imgView.setOnClickListener(new ActivitySwitch(1,this));但仍然無法點擊按鈕..任何解決方案? :( – 2011-03-14 12:18:40

+0

PLZ粘貼你的代碼爲主要活動和ActivitySwitch類 – 2011-03-14 12:25:25

+0

剛剛更新我的問題,thx – 2011-03-14 12:45:55

0

如果這是一個按鈕,簡單地實現按鈕的onclicklistener的功能,並從文本調用各自的活動... ...如果你確實不明白..

+0

您能否向我提供示例代碼的詳細信息? thx – 2011-03-14 12:21:18