上代码 自己的Activity package com.young.android; import com.young.android.annotation.ViewInject; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends YoungActivity { @ViewInject(id = R.id.tv) private TextView textView; @ViewInject(id = R.id.but) private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView.setText("yong inject"); button.setText("注解but"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "注入", 1).show(); } }); } }
<pre name="code" class="html">布局 代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:textSize="20sp" android:layout_height="wrap_content" android:id="@+id/tv"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/but"/> </LinearLayout>
父Activity package com.young.android; import java.lang.reflect.Field; import com.young.android.annotation.ViewInject; import com.young.android.exception.IllegalParameterException; import android.app.Activity; import android.view.View; public class YoungActivity extends Activity { @Override public void setContentView(int layoutResID) { // TODO Auto-generated method stub super.setContentView(layoutResID); Class<? extends YoungActivity> clz = getClass(); Field[] fs = clz.getDeclaredFields(); for (Field f : fs) { ViewInject viewInject = f.getAnnotation(ViewInject.class); if (viewInject != null) { int id = viewInject.id(); View v = findViewById(id); if (v == null) throw new IllegalParameterException( "@ViewInject 参数不合法,你应该这样使用 例:首先在对应的布局文件中定义TextView,id为tv,然后在声明的TextView前标注 @ViewInject(id=R.id.tv)"); f.setAccessible(true); try { f.set(YoungActivity.this, v); } catch (IllegalAccessException e) { // TODO Auto-generated catch block // e.printStackTrace(); throw new IllegalParameterException( "@ViewInject 参数不合法,你应该这样使用 例:首先在对应的布局文件中定义TextView,id为tv,然后在声明的TextView前标注 @ViewInject(id=R.id.tv)"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block // e.printStackTrace(); throw new IllegalParameterException( "@ViewInject 参数不合法,你应该这样使用 例:首先在对应的布局文件中定义TextView,id为tv,然后在声明的TextView前标注 @ViewInject(id=R.id.tv)"); } f.setAccessible(false); } } } }
注解定义 package com.young.android.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD ) @Retention(RetentionPolicy.RUNTIME) public @interface ViewInject { public int id() default 0; }
异常 package com.young.android.exception; public class IllegalParameterException extends RuntimeException{ private static final long serialVersionUID = 839888641617782091L; public IllegalParameterException(String detailMessage) { super(detailMessage); // TODO Auto-generated constructor stub } }
时间: 2024-09-28 11:42:49