2011-04-04 53 views
1

我将我的随机生成的图像解释为按钮变得可点击时遇到问题。每个导致不同的活动。使随机生成的ImageButtons可点击

随机图像实际上是完美的,唯一的问题是它不可点击。

这里是我的Main.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); 

     final List<String> images = new ArrayList<String>(); 
     for (int i=1; i<=13; i++) { 
      images.add("img"+i); 
     } 
     final Button imgView = (Button)findViewById(R.id.top1); 
     String imgName = null; 
     int id = 0; 

     Collections.shuffle(images, new Random()); 
     imgName = images.remove(0); 
     imageRandomizer(imgName, id, imgView); 
    } 

    public void imageRandomizer(String imgName, int id, final Button imgView) { 
     id = getResources().getIdentifier(imgName, 
              "drawable", 
              getPackageName()); 
     imgView.setBackgroundResource(id); 
    } 

} 

在我的布局,我指定的ID top1作为Button。所以上面的代码将查找到我的绘制图像,其中有名称img1.jpgimg2.jpgimg3.jpg,直到img13.jpg

制作一个ImageButton点击一个活动,而不依赖于所显示的随机图像很容易,我可以做到没有问题。 但我想提出的是类似的东西,在生成img1.jpg,就变成可点击的,并导致Activity1.java,为img2.jpg意图去Activity2.java

编辑 @Roflcoptr 这里是我的OnClickListener:

private OnClickListener top_listener = new OnClickListener() { 
     public void onClick(View v) { 
      switch((Integer) v.getTag()) { 

      case 1: 
      Intent aid = new Intent(Main.this, ProjektAID.class); 
      startActivity(aid); 

      case 2: 
      Intent adh = new Intent(Main.this, ProjektADH.class); 
      startActivity(adh); 

      case 3: 
       Intent bos = new Intent(Main.this, ProjektBOS.class); 
       startActivity(bos); 

      case 4: 
       Intent brot = new Intent(Main.this, ProjektBROT.class); 
       startActivity(brot); 

      case 5: 
       Intent care = new Intent(Main.this, ProjektCARE.class); 
       startActivity(care); 

      case 6: 
       Intent caritas = new Intent(Main.this, ProjektCARITAS.class); 
       startActivity(caritas); 

      case 7: 
       Intent doc = new Intent(Main.this, ProjektDOC.class); 
       startActivity(doc); 

      case 8: 
       Intent drk = new Intent(Main.this, ProjektDRK.class); 
       startActivity(drk); 

      case 9: 
       Intent give = new Intent(Main.this, ProjektGIVE.class); 
       startActivity(give); 

      case 10: 
       Intent hive = new Intent(Main.this, ProjektHIV.class); 
       startActivity(hive); 

      case 11: 
       Intent jo = new Intent(Main.this, ProjektJOHANNITER.class); 
       startActivity(jo); 

      case 12: 
       Intent kind = new Intent(Main.this, ProjektKINDERHERZ.class); 
       startActivity(kind); 

      case 13: 
       Intent kult = new Intent(Main.this, ProjektKULTURGUT.class); 
       startActivity(kult); 
      } 
     } 
     }; 

,这里是随机数发生器方法:

public void imageRandomizer(String imgName, int id, final Button imgView) { 
    id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
    imgView.setBackgroundResource(id); 
    imgView.setTag(new Integer(1)); //example for image 1 
    if (imgName.equals("img1")) { 
     imgView.setTag(new Integer(1)); //example for image 1 
    } else if (imgName.equals("img2")) { 
     imgView.setTag(new Integer(2)); 
    } else if (imgName.equals("img3")) { 
     imgView.setTag(new Integer(3)); 
    } else if (imgName.equals("img4")) { 
     imgView.setTag(new Integer(4)); 
    } else if (imgName.equals("img5")) { 
     imgView.setTag(new Integer(5)); 
    } else if (imgName.equals("img6")) { 
     imgView.setTag(new Integer(6)); 
    } else if (imgName.equals("img7")) { 
     imgView.setTag(new Integer(7)); 
    } else if (imgName.equals("img8")) { 
     imgView.setTag(new Integer(8)); 
    } else if (imgName.equals("img9")) { 
     imgView.setTag(new Integer(9)); 
    } else if (imgName.equals("img10")) { 
     imgView.setTag(new Integer(10)); 
    } else if (imgName.equals("img11")) { 
     imgView.setTag(new Integer(11)); 
    } else if (imgName.equals("img12")) { 
     imgView.setTag(new Integer(12)); 
    } 
    else if (imgName.equals("img13")) { 
     imgView.setTag(new Integer(13)); 
    } 
} 
+1

你缺少switch/case结构中的'break'语句。 – 2011-04-04 10:05:38

+0

非常感谢您的提醒。我的坏 – hectichavana 2011-04-04 11:23:16

回答

1

我会使用标签来标识按钮。因此,在您的imateRandomizer中为每个可能的图像添加一个唯一的ID。我不知道你怎么能唯一标识的图像,但我会在这里显示的例子的名字:

public void imageRandomizer(String imgName, int id, final Button imgView) 
    { 
     id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
     imgView.setBackgroundResource(id); 

     if (imgName.equals("Name of the Image for your first activity") { 
      imgView.setTag(new Integer(1)); //example for image 1 
     } else if (imgName.equals("Name of the Image for your second activity") { 
      imgView.setTag(new Integer(2)); 
     } 
    } 

,然后在你的ClickListener您可以检查该按钮具有标签:

imgView.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       switch((Integer) v.getTag()) { 

         case 1: //start Activity 1; 

         break; 

         case 2: //start Activity 2; 

         break; 
       } 
      } 
     }); 
+0

这并没有帮助,只有情况13被读取。 13是随机图像收集的总量 – hectichavana 2011-04-04 08:34:30

+0

您能展示如何设置和阅读标签吗? – 2011-04-04 08:35:51

+0

我编辑了我的问题,你可以查看它 – hectichavana 2011-04-04 08:53:34