Android Control Injection

Source: Internet
Author: User

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> * Just let the custom activity inherit the supportactivity, Then let the control field name of the custom activity be the same as the ID name of the control in the loaded layout file, so that after calling Setcontentview (view view) or Setcontentview (int layoutresid), The control field is injected into the value, and you can then invoke the method owned by the control field. </br></br> * If the Android.view.View.OnClickListener interface is implemented on the custom activity at the same time, the replication public void OnClick is automatically called when the control is clicked (View V) method * @author Young * @Time 2015-7-4 */public class Supportactivity extends Activity {public void Setcontentview (i NT Layoutresid) {Setcontentview (Layoutinflater.from (this). Inflate (Layoutresid, null));}; @Overridepublic void Setcontentview (view view) {//TODO auto-generated method Stubsuper.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", Packag ENAME); 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; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//No need for the following so wordy disgusting operation//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 ());




<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:orien tation= "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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Control Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.