Android获取视图的types

我怎样才能做到这一点?

东西:

final View view=FLall.getChildAt(i); if (view.getType()==ImageView) { ... } 

如果出于某种奇怪的原因,你不能使用朝日的build议(使用标签),我的build议是:

 if (view instanceof ImageView) { ImageView imageView = (ImageView) view; // do what you want with imageView } else if (view instanceof TextView) { TextView textView = (TextView) view; // do what you want with textView } else if ... 

我尝试以下,它的工作:

 View view=FLall.getChildAt(i); Log.i("ViewName", view.getClass().getName()); 

对于其他谁检查这个问题,在某些情况下, instanceof不起作用(我不知道为什么!),例如,如果你想检查视图types是ImageViewImageButton (我testing了这种情况),它得到他们一样,所以你用这种方式扫描:

 //v is your View if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageView")) { Log.e("imgview", v.toString()); imgview = (ImageView) v; } else if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageButton")) { Log.e("imgbtn", v.toString()); imgbtn = (ImageButton) v; } 

你可以使用标签:参见http://developer.android.com/reference/android/view/View.html中的; set / getTag方法