package com.example.smartinject; import java.lang.reflect.Field; import java.lang.reflect.Method; import android.app.Activity; import android.content.res.Resources; import android.view.LayoutInflater; import android.view.View; /** * 使用说明:</br> * 只需让自定义Activity继承该SupportActivity,然后让自定义Activity的控件字段名称和加载的布局文件中控件的id名称相同即可, * 这样在调用setContentView(View view)或setContentView(int layoutResID)后,控件字段就被注入值了,之后就可以调用控件字段所拥有的方法了。</br></br> * 若同时在自定义Activity上实现了android.view.View.OnClickListener接口后,点击控件后就会自动调用复写的public void onClick(View v)方法了 * @author Young * @Time 2015-7-4 */ public class SupportActivity extends Activity { public void setContentView(int layoutResID) { setContentView(LayoutInflater.from(this).inflate(layoutResID, null)); }; @Override public void setContentView(View view) { // TODO Auto-generated method stub super.setContentView(view); smartInject(); } private void smartInject() { Class<? extends Activity> clz=getClass(); Field[] fs=clz.getDeclaredFields(); Resources res = getResources(); String packageName = getPackageName(); for (Field field : fs) { int viewId = res.getIdentifier(field.getName(), "id", packageName); if (viewId==0) continue; field.setAccessible(true); try { View v=findViewById(viewId); field.set(this,v); Class<?> c=field.getType(); Method m=c.getMethod("setOnClickListener", android.view.View.OnClickListener.class); m.invoke(v, this); } catch (Exception e) { e.printStackTrace(); } field.setAccessible(false); } } }
<pre name="code" class="java">package com.example.smartinject; import java.util.Date; 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 SupportActivity implements OnClickListener{ Button but1; Button but2; Button but3; Button but4; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 再也不需要下面这么啰嗦恶心的操作了 // but1=(Button) findViewById(R.id.but1); // but2=(Button) findViewById(R.id.but2); // but3=(Button) findViewById(R.id.but3); // but4=(Button) findViewById(R.id.but4); // tv=(TextView) findViewById(R.id.tv); // but1.setOnClickListener(this); // but2.setOnClickListener(this); // but3.setOnClickListener(this); // but4.setOnClickListener(this); // tv.setOnClickListener(this); but1.setText("inject ok"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.tv: Toast.makeText(getApplicationContext(), "inject", 1).show(); tv.setText("tv click "+new Date().toLocaleString()); break; case R.id.but1: Toast.makeText(getApplicationContext(), "inject", 1).show(); but1.setText("but click "+new Date().toLocaleString()); break; default: ((TextView)v).setText(""+v.toString()+" "+System.currentTimeMillis()); break; } } }
<LinearLayout 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" android:orientation="vertical" > <Button android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/but1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> <Button android:id="@+id/but2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> <Button android:id="@+id/but3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> <Button android:id="@+id/but4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> </LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/qingchunweiliang/article/details/46757599