AndroidJAVA中如何利用泛型使方法动态返回不同类型

    xiaoxiao2021-03-25  113

    比如我们在开发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; } }
    转载请注明原文地址: https://ju.6miu.com/read-9424.html

    最新回复(0)