android通過反射根據包名和id或name擷取resource ID__resourceId

來源:互聯網
上載者:User

        android裡面,不管是layout,widget,drawable,string,color,array,style等等,android自動會在R.java裡面產生resouce ID。那如果只給你包名和widget id 或者string的name,你能得到產生的resouce ID,進而擷取這個view或者string嗎。實事是可以的。舉個栗子給大家看看。

       

 /**     * 得到資源檔.     *      * @param packageName     *            包名     * @param typeName     *            資源類型     * @param instenceName     *            資源名     * @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;    }
     擷取string

     

    Toast.makeText(this,getResources().getString(getResourceId("com.figo.study","string","cp_need_agree_protocol")),                    Toast.LENGTH_SHORT).show();
    擷取view

    

  card_number = (TextView) findViewById(getResourceId("com.figo.study", "id", "tv_card_num"));
     為什麼可以這麼做。是因為R.java裡面儲存了資源的resource id,而且是個內部靜態類,通過反射以後就可以擷取裡面的resouce id了,可以看看R.java是長怎麼樣的。

       

/* AUTO-GENERATED FILE.  DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found.  It * should not be 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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.