我正在添加一个ImageViews数组,并动态地为每个ImageView设置一个图像,我已经完成了。但问题是如何在ImageView上设置/定义onclicklistener方法?
这是我的代码:

ImageView[] mImages;
int[] images={R.drawable.sandle_icon1, R.drawable.sandle_icon2,
            R.drawable.sandle_icon3, R.drawable.sandle_icon4};

LinearLayout ll = new LinearLayout(this);
mScrollViewImage.removeAllViews();
ll.setOrientation(LinearLayout.VERTICAL);
mImages = new ImageView[images.length];
mScrollViewImage.addView(ll);
for (floop = 0; floop < sandleicon.length; floop++) {
    mImages[floop] = new ImageView(this);
    mImages[floop].setImageResource(images[floop]);
        ll.addView(mImages[floop]);
}

任何帮助都将不胜感激。

最佳答案

for (floop = 0; floop < sandleicon.length; floop++) {
    mImages[floop] = new ImageView(this);
    mImages[floop].setImageResource(images[floop]);
    mImages[floop].setId(floop);
    ll.addView(mImages[floop]);
    mImages[floop].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //v.getId() will give you the image id
        }
    });
}

08-06 08:56