How to check the type of a view widget?
How to get that working:
if(findViewById(ResIdWithUnkownType) instanceof Bitmap)
{
Bitmap bitmap = (Bitmap) fi开发者_运维知识库ndViewById(ResIdWithUnkownType);
} else if(findViewById(ResIdWithUnkownType) instanceof ImageView)
{
ImageView = (ImageView) findViewById(ResIdWithUnkownType);
}
The second block would work just fine. The problem is the first one: findViewById
returns a View
object always, and Bitmap
is not a View
, so the first if
statement will never be executed.
This is not the answer but For Others who check this Question,in some cases instanceof
does not work(I do not know why!),for example if your want to check if view type is ImageView
or ImageButton
(i tested this situation) , it get them the same, so you scan use this way :
//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;
}
精彩评论