使随机生成的 ImageButtons 可点击

发布于 2024-10-29 18:36:07 字数 4841 浏览 1 评论 0原文

我无法使随机生成的图像(被解释为按钮)变得可点击。每一个都会导致不同的活动。

随机图像实际上工作得很好,唯一的问题是不可点击。

这是我的 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));
    }
}

I'm having trouble with making my randomly generated image, which is interpreted as a button, become clickable. Each leads to a different activity.

The random images work perfect actually, the only problem it's not clickable.

Here's my 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);
    }

}

On my layout, I specified the id top1 as a Button. So the above code will look up to my drawable images, which have the names img1.jpg, img2.jpg, img3.jpg , until img13.jpg.

Making an ImageButton clickable to one activity without being dependent on the shown random image is easy, I can do it without problem.
But what I wanna make is something like, when img1.jpg is generated, it becomes clickable and leads to Activity1.java, for img2.jpg the intent goes to Activity2.java, etc.

EDIT
@Roflcoptr
Here's my 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);
             }
        }
       };

and here's the randomizer method:

 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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2024-11-05 18:36:07

我会使用标签来识别按钮。因此,在您的 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;
                 }
             }
         });

I would use a tag to identify the button. So in your imateRandomizer add a unique ID for each possible Image. I don't know how you can identify the images uniquely, but I'll show the example here for the name:

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));
        }
    }

And then In your ClickListener you can check which tag the button has:

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;
                 }
             }
         });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文