Android obtains resource Id__resourceid by reflection based on package name and ID or name

Source: Internet
Author: User
Tags reflection static class

Android automatically generates resouce IDs inside R.java, whether it's layout,widget,drawable,string,color,array,style and so on. So if you just give you the package name and widget ID or string name, you can get the generated Resouce ID and get the view or string. The truth is possible. Give us a chestnut to see.

/**
     * Get resource files.
     * 
     @param packagename *
     @param typeName *            Resource type *
     @param instencename            * Resource Name
     * @return int
     *
    /public static int Getresourceid (string packagename, String typeName,
            string Instencename) {
        if (packagename!= null && typeName!= null && instencename!= null) {
            try {
  class<?> cl = class.forname (PackageName + "$" + typeName);
                Field field = Cl.getfield (instencename);
                Object obj = Field.get (Cl.newinstance ());
                Return Integer.parseint (Obj.tostring ());
            } catch (Exception e) {
                e.printstacktrace ();
            }
        }
        return-1;
    }
Get string

    Toast.maketext (This,getresources (). GetString (Getresourceid ("Com.figo.study", "string", "Cp_need_agree_protocol") ),
                    Toast.length_short). Show ();
Get view

  Card_number = (TextView) Findviewbyid (Getresourceid ("Com.figo.study", "id", "tv_card_num"));
Why do you do that? is because the R.java inside the resource resource ID, and is an internal static class, through reflection can get inside the Resouce ID, can see how R.java is long.

/* auto-generated FILE.
 Do not MODIFY.  * This class is automatically generated by the * AAPT tool from the resource data it found.
 It * should is modified by hand.

* * Package com.figo.study;
		Public final class R {public static final class Anim {public static final int cpay_push_left_in = 0x7f040000;
	public static final int cpay_push_left_out = 0x7f040001;
	public static final class array {public static final int cp_auth_idtype = 0x7f050000;
		public static final class color {public static final int cp_light_white = 0x7f060000;
	public static final int cp_red = 0x7f060001;
	public static final class Dimen {public static final int dialog_prefered_width = 0x7f070000;
		public static final class Drawable {public static final int background_dialog = 0x7f020000;
		
	public static final int btn_submit_selector = 0x7f020001;
		public static final class ID {public static final int tv_card_num = 0x7f0a0038; public static final int btn_help = 0x7f0a0010;
		public static final class layout {public static final int bosh_debitcard_auth = 0x7f030000;
	public static final int bosh_dialog_progress = 0x7f030001;
		public static final class string {public static final int cp_need_agree_protocol = 0x7f080039;
	public static final int cpay_keyboard_letter = 0x7f080033;
		public static final class style {public static final int appbasetheme = 0x7f090000;
	public static final int apptheme = 0x7f090001;
 }
}

Related Article

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.