自定义注解,仿butterknife,找控件点击事件
来源:互联网 发布:矩阵乘法怎么算 编辑:程序博客网 时间:2024/06/05 11:53
activity_butter.xml里面写按钮
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.menglucywhh.day1128_create.butterknife.ButterActivity"> <Button android:text="button1" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="button2" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="button3" android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="button4" android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>自定义注解的类IBindView
@Documented@Target(ElementType.FIELD)//在属性上注解@Retention(RetentionPolicy.RUNTIME)//自己写的butterknifepublic @interface IBindView { int value();}自定义注解的类,点击事件.IOnclick
@Documented@Target(ElementType.METHOD)//在方法上注解@Retention(RetentionPolicy.RUNTIME)public @interface IOnclick { int value();}ButterActivity.java里面通过反射自定义注解
import android.app.Activity;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.example.menglucywhh.day1128_create.R;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class ButterActivity extends AppCompatActivity { @IBindView(R.id.button1) Button button1; @IBindView(R.id.button2) Button button2; @IBindView(R.id.button3) Button button3; @IBindView(R.id.button4) Button button4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butter); //调用找到 id和设置点击事件的方法 initClick(this); button2.setText("按钮1"); button3.setText("按钮2"); } //找id和设置点击事件的方法 private static void initClick(final Activity activity) { //获取到当前的类 Class clazz = activity.getClass(); //获取当前类的所有属性 , Field[] fields = clazz.getDeclaredFields(); //遍历属性 for (Field field:fields){ //判断当前属性 身上是否有ibindview注解 if(field.isAnnotationPresent(IBindView.class)){ //获取当前属性身上的注解 IBindView iBindView = field.getAnnotation(IBindView.class); //查找控件,根据这个id查出控件 View view = activity.findViewById(iBindView.value()); //把view设置给当前的属性 try { field.set(activity,view); //获取当前类的public方法 Method[] methods = clazz.getMethods(); //遍历方法 for (final Method method:methods){ //判断当前方法上是否有ionclick注解 if(method.isAnnotationPresent(IOnclick.class)){ //获取方法上的注解 IOnclick click = method.getAnnotation(IOnclick.class); //获取注解值 int id = click.value(); //如果点击事件上面的注解id和 上面的id一样.,触发点击事件 if(id == iBindView.value()){ view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { //调用下面写好的点击事件 method.invoke(activity); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }); } } } } catch (IllegalAccessException e) { e.printStackTrace(); } } } } //自己加的注解 @IOnclick(R.id.button2) public void onViewClicked(){ Toast.makeText(this, "button2", Toast.LENGTH_SHORT).show(); }}
阅读全文
0 0
- 自定义注解,仿butterknife,找控件点击事件
- Android注解解析,注解用法,仿xUtils用注解初始化控件、点击事件(一)
- Android注解解析,注解用法,仿xUtils用注解初始化控件、点击事件(二)
- Android注解解析,注解用法,仿xUtils用注解初始化控件、点击事件(一)
- Android注解解析,注解用法,仿xUtils用注解初始化控件、点击事件(二)
- ButterKnife点击事件无效或控件绑定为空
- ButterKnife的使用以及自定义注解实例化控件
- 自定义自定义控件的点击事件
- Android自定义注解与注解器实现点击事件绑定
- Android 中注解view (仿butterknife)
- 自定义带点击事件spiner控件
- 自定义控件实现不规则区域点击事件
- Qt自定义控件---QLabel点击事件
- 自定义view中控件的点击事件
- 给自定义控件中的子控件自定义点击事件
- Android注解找控件
- Butterknife 绑定控件后,点击事件判断为空,问题解决方法一
- 自定义组合控件,自定义View,接口点击事件
- HDOJ1049 Climbing Worm
- 51nod 1299 监狱逃离
- .gitignore 重新激活
- 1.3计算机网络的分类
- 2017-11-25前端初识协会分享会
- 自定义注解,仿butterknife,找控件点击事件
- <01读书回忆录
- C#构造函数里的base和this的区别
- 网络接口,与网络桥接的管理。
- [PAT]1033. To Fill or Not to Fill (25)(Java实现)
- MOOC微软课程:深度神经网络在语音识别上的应用
- 88. Merge Sorted Array 合并有序数组
- 2017传智播客python基础班+就业班
- ubuntu 下出现E: Sub-process /usr/bin/dpkg returned an error code