Android Control Injection and Android Control
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;/*** instructions for use: </br> * you only need to let the custom Activity inherit the SupportActivity, then, let the control field name of the custom Activity be the same as the control id name in the loaded layout file. * after setContentView (View view) or setContentView (int layoutResID) is called, the control field is injected with a value, and then the control can be called. The method of the field. </Br> * If android is enabled on the custom Activity. view. view. after you click the OnClickListener interface, the public void onClick (View v) method * @ author Young * @ Time 2015-7-4 */public class SupportActivity extends Activity {public void setContentView (int layoutResID) {setContentView (LayoutInflater. from (this ). inflate (layoutResID, null) ;};@ Overridepublic void setContentView (View view) {// TODO Auto-generated method stubsuper. s EtContentView (view); smartInject ();} private void smartInject () {Class <? Extends Activity> clz = getClass (); Field [] fs = clz. getDeclaredFields (); Resources res = getResources (); String packageName = getPackageName (); for (Field 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 suppactivity activity implements OnClickListener {Button but1; Button but2; Button but3; Button but4; TextView TV; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // you no longer need the following nasty operations: // 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") ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (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>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.