比如我们在开发Android的时候,每次findViewById(R.id.xxx) 之后都要强制转换为自己想要的组件 假如写一个如下这样的方法,那么到时候直接调用此方法赋值给相关View变量即可 如: private TextView textView1; textView1 = findView(R.id.textView1); //那么就不用强制转换为TextView了 public <T> T findView(int resId) { if (act == null) return null; try { return (T) act.findViewById(resId); } catch (Exception e) { e.printStackTrace(); return null; } }
Android JAVA中如何利用泛型使方法动态返回不同类型
最新推荐文章于 2024-04-21 18:18:42 发布