[英]How do I make a method in my Java class return findViewById?
I gave my layout file every single element an ID, and in my java class I have a method called locate().我给我的布局文件中的每个元素一个 ID,在我的 java class 中我有一个名为 locate() 的方法。 What I'm going to return is:我要返回的是:
return findViewById(R.id.star);
But it says cannot resolve symbol findViewById.但它说无法解析符号 findViewById。 Also I don't know what should I declare as return type for the method.另外我不知道我应该将什么声明为该方法的返回类型。
So here's what my code look like.所以这就是我的代码的样子。
public class Tank {
public int x;
public int y;
public String face;
public Tank(int xPos,int yPos){
x=xPos;
y=yPos;
}
public RETURNTYPE locate(){
return findViewById(R.id.star);
}
}
I don't know what should be the return type, and findViewById cannot be resolved.不知道返回类型应该是什么,findViewById也无法解析。 I created this class manually(not auto generated).我手动创建了这个 class(不是自动生成的)。
findViewById
is a method of a View
, as well of an Activity , hence you'll need an instance of View
or of Activity
to call it. findViewById
是View
和Activity的方法,因此您需要View
或Activity
的实例来调用它。
Probably it works for you now because you're inside an Activity or a View, but once you move it somewhere else, you'll need to call it on said activity or view, ie:可能它现在对您有用,因为您在 Activity 或 View 中,但是一旦将其移动到其他地方,您就需要在所述 Activity 或视图上调用它,即:
View locate(Activity activity) {
return activity.findViewById(R.id.star);
}
View locate(View view) {
return view.findViewById(R.id.star);
}
Notice I'm returning View
as the return type?请注意,我将View
作为返回类型返回? Although this is not incorrect, it's actually not the best and we can make it better.虽然这不是错误的,但它实际上并不是最好的,我们可以做得更好。
If you notice, finViewById
returns a generic type that extends View
.如果您注意到, finViewById
返回一个扩展View
的泛型类型。 In other words, a concrete view and therefore we could rewrite the methods above like:换句话说,一个具体的视图,因此我们可以重写上面的方法,如:
<T extends View> T locate(Activity activity) {
return activity.findViewById(R.id.star);
}
<T extends View> T locate(View view) {
return view.findViewById(R.id.star);
}
Why is this better?为什么这样更好? Because you won't have to cast it to the right thing.因为您不必将其投射到正确的位置。 Without generics you would have to call it like:如果没有 generics 你就不得不这样称呼它:
TextView t = (TextView) locate();
With generics the code becomes more clean:使用 generics 代码变得更加干净:
TextView t = locate();
PS: Back some time ago, findViewById
actually returned View
and we were all casting the return type. PS:回到前段时间, findViewById
实际上返回了View
,我们都在强制转换返回类型。 Nowadays, Google changed it for generics and we don't need to.如今,谷歌将其更改为 generics,我们不需要。
You need proper context when using the findViewById()
like so:使用findViewById()
时需要适当的上下文,如下所示:
public <T extends View> T locate(Activity ctx){
return ctx.findViewById(R.id.star);
}
No need to take Activity
or Context
for finding the View
.无需使用Activity
或Context
来查找View
。 As every view which you will locate by findViewById()
will be a child of any ViewGroup
.因为您将通过findViewById()
找到的每个视图都将是任何ViewGroup
的子视图。 So just make it所以就做吧
public <T extends View> T locate(View parentView){
return parentView.findViewById(R.id.star);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.