zoukankan      html  css  js  c++  java
  • android 通过 名称获取图片

    引用:http://www.devdiv.com/thread-83839-1-1.html

    一般情况下用不到,使用于特殊情况。

    直接贴代码。

    01 public class ReadyDrawable extends Activity {
    02     /** Called when the activity is first created. */
    03     private Button btn;
    04     private ImageView iv;
    05      
    06     @Override
    07     public void onCreate(Bundle savedInstanceState) {
    08         super.onCreate(savedInstanceState);
    09         setContentView(R.layout.main1);
    10          
    11         iv = (ImageView)findViewById(R.id.tp);
    12         btn = (Button)findViewById(R.id.an);
    13         btn.setOnClickListener(new MyOnClickListener());
    14          
    15     }
    16      
    17      
    18     /**
    19      * 通过java反射机制反射出R.drawable类中的属性,因为都是静态常量所以可以获取属性对应的值。
    20      * @author Administrator
    21      *
    22      */
    23     public class MyOnClickListener implements OnClickListener{
    24          
    25         @SuppressWarnings("unchecked")
    26         public void onClick(View v) {
    27             Class drawable  =  R.drawable.class;
    28             Field field = null;
    29             try {
    30                 field = drawable.getField("icon");
    31                 int r_id = field.getInt(field.getName());
    32                 iv.setBackgroundResource(r_id);
    33             catch (Exception e) {
    34                 Log.e("ERROR""PICTURE NOT FOUND!");
    35             }
    36         }
    37          
    38     }
    39      
    40      
    41 }
  • 相关阅读:
    基于Windows Mobile 5.0的GPS应用程序开发
    iis6应用程序池被自动禁用问题 应用程序池'DefaultAppPool' 被自动禁用
    Axapta物流模块深度历险(八)
    AX的报表服务器(一)
    AX的企业门户(一)
    SQL Server2005 Reporting Services 管理站点
    SQL Server2005 Reporting Services的卸载
    耐心的解决问题
    危险操作符
    慢慢的坚持啊
  • 原文地址:https://www.cnblogs.com/sode/p/2759868.html
Copyright © 2011-2022 走看看