findViewById动态强制转换为func?

时间:2013-07-18 17:33:40

标签: java android

我想创建一个自动投射 findViewById-View-from-reference 的函数。 我能以某种方式实现这一目标吗?

private View getReferenceForView(View view, int resId)
      {
         view =  (view.getClass())findViewById(resId);


          return view;
      }
那里不接受

view.getClass(),但是我想在电话中找到这样的内容:

myView = getReferenceForView(myView, R.drawable.someresid);

所以我可以在不使用annyoing演员的情况下获得我的观点的参考。

1 个答案:

答案 0 :(得分:1)

使用您想要的方法的唯一方法是使用反射instanceof。但这导致了Android中每个可用View的巨大if-else块。

您将拥有以下内容:

private View getReferenceForView(View view, int resId)
{
     if(view instanceof TextView)
         view = view.findViewById(resId);

     else if(view instanceof EditText)

     // and so on..

     return view;
  }

我不知道你的确切方法,但我认为这不值得努力。

相关问题