首页 > 移动平台 > 详细

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

时间:2015-12-05 15:57:34      阅读:336      评论:0      收藏:0      [点我收藏+]
 1     @SuppressWarnings("unchecked")
 2     public <T extends Fragment> T getFragment(int id) {
 3         T result = (T)getFragmentManager().findFragmentById(id);
 4         if (result == null) {
 5             throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
 6                     + " doesn‘t exist");
 7         }
 8         return result;
 9     }
10 
11     /**
12      * Convenient version of {@link #findViewById(int)}, which throws
13      * an exception if the view doesn‘t exist.
14      */
15     @SuppressWarnings("unchecked")
16     public <T extends View> T getView(int id) {
17         T result = (T)findViewById(id);
18         if (result == null) {
19             throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
20                     + " doesn‘t exist");
21         }
22         return result;
23     }

 

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

原文:http://www.cnblogs.com/ayixi/p/5021633.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!