Activity上的注解实现
2015-06-06 10:22
183 查看
Activity上的注解实现
注解的基础知识
@Target(用于注解属性或方法):ElementType.FIELD、ElementType.METHOD@Retention(处理的时机):RetetionPolicy.SOURCE、RetetionPolicy.CLASS、RetetionPolicy.RUNTIME
注解的实现步骤
### 1、实现注解类// 用于注解属性 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FindView { int value() default 0; }
// 用于注解方法 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Onclick { int value() default 0; }
2、解读注解方法类
public class ViewUtils { /* * 注解类的实现主要是通过反射机制去实现 */ public static void register(Activity activity) { registerField(activity); registerMethod(activity); } public static void registerField(Activity activity) { // 通过反射机制,找到类的所有属性 Class<?> clazz = activity.getClass(); Field[] fields = clazz.getDeclaredFields(); try { for (Field field : fields) { // 判断属性中是否有相应的注解 if (field.isAnnotationPresent(FindView.class)) { // 获取注解类中的value值,即view的id值 FindView findView = field.getAnnotation(FindView.class); int viewId = findView.value(); if (viewId > 0) { // 通过id值找到相应的view,并进行赋值 field.setAccessible(true); field.set(activity, activity.findViewById(viewId)); } } } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } } public static void registerMethod(final Activity activity) { // 通过反射机制,找到类的所有方法 Class<?> clazz = activity.getClass(); Method[] methods = clazz.getDeclaredMethods(); for (final Method method : methods) { // 判断方法中是否有相应的注解 if (method.isAnnotationPresent(Onclick.class)) { // 获取注解类中的value值,即view的id值 int viewId = method.getAnnotation(Onclick.class).value(); if (viewId > 0) { // 通过id值找到相应的view,并添加事件监听 final View view = activity.findViewById(viewId); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { method.invoke(activity, view); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }); } } } } }
注解的测试类
public class MainActivity extends Activity { @FindView(R.id.btn_first) private Button btnFirst; @FindView(R.id.btn_second) private Button btnSecond; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewUtils.register(this); } @Onclick(R.id.btn_first) public void onFirstClick(View view) { btnFirst.setText("我是第一个按钮"); Toast.makeText(this, "哈哈哈!", Toast.LENGTH_SHORT).show(); } @Onclick(R.id.btn_second) public void onSecondClick(View view) { btnSecond.setText("俺不只是一个按钮"); Toast.makeText(this, "俺被点击了啊!", Toast.LENGTH_SHORT).show(); } }
测试类的布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="first" /> <Button android:id="@+id/btn_second" android:layout_below="@+id/btn_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" /> </RelativeLayout>
相关文章推荐
- 主页木马的制作方法
- 解密网页加密的两个方法
- 定义标题的最好方法
- Ruby简明教程之方法(Method)介绍
- Ruby基础知识之方法、代码段
- C#获取文件夹及文件的大小与占用空间的方法
- Delphi控件ListView的属性及使用方法详解
- VB6实现连接Access数据库的ADODB代码实现方法
- MySQL重定位数据目录的方法
- PowerShell中Get-Date对象的常用属性和方法介绍
- 在Linux上运行C#的方法
- C#实现绘制面形图表的方法详解
- ruby 流程控制 方法
- CSS清除浮动常用方法小结
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- Oracle中插入特殊字符:&和'的解决方法汇总
- C#获取字符串后几位数的方法
- javascript下拉框不被选中大类的区别方法
- MySQL数据库安全之防止撰改的方法
- 详解链接的rel与target区别