我有顯示4個ImageViews(所有照片,收藏夾,Taged,專輯)的自定義對話框。獲取ImageView而不知道它
佈局是這樣的:
<LinearLayout>
<ImageView>...1</ImageView>
<ImageView>...2</ImageView>
<ImageView>...3</ImageView>
<ImageView>...4</ImageView>
</LinearLayout>
構造函數包含以下代碼:
iv = new ImageView[3];
iv[0] = (ImageView) findViewById(R.id.btnPhotoAll);
iv[1] = (ImageView) findViewById(R.id.btnPhotoFavorites);
iv[2] = (ImageView) findViewById(R.id.btnPhotoTags);
iv[3] = (ImageView) findViewById(R.id.btnPhotoAlbums);
for (ImageView image : iv){
image.setOnClickListener(new DialogPhotoOnItemClickListener(context, image));
}
的listener:
package com.kappa.cloudgallery.listeners;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.kappa.cloudgallery.R;
import com.kappa.cloudgallery.widgets.DialogPhoto;
public class DialogPhotoOnItemClickListener implements View.OnClickListener
{
Context context;
ImageView iv;
public DialogPhotoOnItemClickListener(Context context, ImageView imageView){
this.context = context;
this.iv = imageView;
}
@Override
public void onClick(View view)
{
switch (iv.getId())
{
case R.id.btnPhotoAll:
Toast.makeText(context, "All", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoFavorites:
Toast.makeText(context, "Favorites", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoTags:
Toast.makeText(context, "Tags", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoAlbums:
Toast.makeText(context, "Albums", Toast.LENGTH_LONG).show();
break;
}
}
}
的問題是,我通過ImageView的在構造函數中。有什麼有效的方法來獲取我點擊的圖像?
對話框:enter image description here
傳遞ID代替? –
是的!但它會是一樣的。這個想法是不傳遞任何東西,並使用'onClick'事件上的'查看視圖'來查找它。如果有任何想法! –
那麼,你必須有一個ID的一些參考,否則你不能比較點擊的視圖的ID到任何東西 –