6

我想通过单击动态创建的图像来获取图像名称......

 for(int i=0;i<list.size();i++)
    {
        final ImageButton b=new ImageButton(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
        layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom
        b.setLayoutParams(layoutParams);
        Resources res = getResources();
        int resourceId = res.getIdentifier("_"+list.get(i).toString(), "drawable", getPackageName() );
        b.setImageResource( resourceId );
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
              //at here i want to get the selected image name


            }
        });
        rowoptions.addView(b);
    }    
4

1 回答 1

1

将图像的名称设置为ImageButton标签。然后,您可以在onClick(). 使用setTag()getTag()为此:

    String imageName = "_" + list.get(i).toString();
    int resourceId = res.getIdentifier(imageName, "drawable", getPackageName() );
    b.setTag(imageName);
    b.setImageResource(resourceId);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          //at here i want to get the selected image name
          String clickedImageName = (String)v.getTag();

        }
    });
于 2013-02-25T13:59:59.573 回答