android通过泛型获取控件或视图

     @SuppressWarnings("unchecked")
public <T extends Fragment> T getFragment(int id) {
T result = (T)getFragmentManager().findFragmentById(id);
if (result == null) {
throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
+ " doesn't exist");
}
return result;
} /**
* Convenient version of {@link #findViewById(int)}, which throws
* an exception if the view doesn't exist.
*/
@SuppressWarnings("unchecked")
public <T extends View> T getView(int id) {
T result = (T)findViewById(id);
if (result == null) {
throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
+ " doesn't exist");
}
return result;
}
上一篇:fir.im Weekly - 揭秘直播移动 APP 技术实现


下一篇:javascript操作元素的css样式