android 下动态获取控件的id

有时候我们需要动态的取得一个一个控件的id,然后进行操作,经过在网上查找,找到了一下方法getResources().getIdentifier("textView01", "id", "cn.xxx.xxx");

第一个参数为ID名,第二个为资源属性是ID或者是Drawable第三个为包名

以下是从网上找到资料:
主要由两种方法,个人建议第二种。 
1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为: 
String path = "com/drawable/resource/image.png"; 
InputStream is = getClassLoader().getResourceAsStream(path); 
Drawable.createFromStream(is, "src"); 

2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了: 
假设创建工程的时
候,填写的package名字为:com.test.image 
int resID = getResources().getIdentifier("goto_radar", "drawable", "com.test.image"); 
Drawable image = getResources().getDrawable(resID);

原文地址:https://www.cnblogs.com/androidxiaoyang/p/3772235.html