Android Reflection Resource Tool Reflectionutil

Source: Internet
Author: User

Android Reflection Resource Tool Reflectionutil

    • Android Reflection Resource Tool Reflectionutil
      • Tool code
      • Tool use

Tool code

Reflectionutil

 Public  class reflectionutil {     Public enumResourcestype {styleable, Style, String, Mipmap, menu, Layout, Integer, ID, drawable, dimen, color, bool, attr, Anim}/** * By name, Reflection gets the resource * * @param context * @param Name Resources name *
     
       @param type enum of Resourcestype *
       @return Resources ID *
      /     Public Static int Getresourceid(context context, String name, Resourcestype type) {String className = context.getpackagename () +". R ";Try{class<?> c = class.forname (ClassName); for(Class childclass:c.getclasses ()) {String simplename = Childclass.getsimplename ();if(Simplename.equals (Type.Name ())) { for(Field field:childClass.getFields ()) {String fieldName = Field.getname ();if(fieldname.equals (name)) {Try{return(int) Field.get (NULL); }Catch(Illegalaccessexception e)                            {E.printstacktrace (); }                        }                    }                }            }        }Catch(ClassNotFoundException e)        {E.printstacktrace (); }return-1; }}
Tool use

Reflectionutilactivity

 Public  class reflectionutilactivity extends appcompatactivity implements  View. Onclicklistener {ImageView ImageView;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); This. Setcontentview (R.layout.activity_reflection_util); This. ImageView = (ImageView) This. Findviewbyid (R.id.reflection_iv); This. Findviewbyid (R.ID.REFLECTION_BT). Setonclicklistener ( This); }/** * Called when a view has been clicked.     * * @param v The view that is clicked. */    @Override     Public void OnClick(View v) {Switch(V.getid ()) { CaseR.ID.REFLECTION_BT:/ * * Get mipmap Type file with resource named Mm_1 * /                 This. Imageview.setimageresource (Reflectionutil.getresourceid ( This,"Mm_1", ReflectionUtil.ResourcesType.mipmap)); Break; }    }}

Android Reflection Resource Tool Reflectionutil

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.