Why are parentheses used when declaring a textview
I'm fairly new to android. I've got experience with c# and java but I can't figure this out.
I know how to use it. I've written a couple apps already. It's probably really simple but I don't understand this. Why is (TextView) in parentheses.
I've searched the sdk documentation and afaik it doesn't mention this.
TextView example = (Te开发者_StackOverflow中文版xtView) findViewById(R.id.example);
This is an explict cast, as findViewById() returns the more generic type View.
The convention is from C, and then used in C++, and in this case, Java. VB.net and other BASIC variants have CType(), or CAST(). Most stongly typed languages have some similar mechanism to explicitly convert between types in a compatible way. Note that, in example, not everything returned from findViewById() can be safely cast into the TextView type.
精彩评论