可能重複:
Android, getting resource ID from string?如何獲得資源從字符串(INT) - Android電子
String res = "R.drawable.image1";
我需要得到它的INT ID。請幫幫我!
可能重複:
Android, getting resource ID from string?如何獲得資源從字符串(INT) - Android電子
String res = "R.drawable.image1";
我需要得到它的INT ID。請幫幫我!
使用此獲得資源ID:
int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());
View view = findViewById(R.drawable.image1);
int id = view.getId();
這將返回'R.drawable.image1' ...您使用了一個您已經擁有的身份證,然後找到它,然後將其取回 – 2016-06-02 18:05:04
這不會回答使用身份驗證的問題一個字符串,因爲你在findViewById中對它進行硬編碼 – Alejandro 2016-07-31 18:55:54
如果你想獲得integer
然後簡單地刪除引號。
int id = R.drawable.image1; // instead of "R.drawable.image1"
這會給你的號碼。
沒有任何方法像* getApplicationPackageName()*,而沒有看到答案如何可以upvote,啊。 – 2012-04-16 08:51:13
對不起我的錯我已更新答案 – Nishant 2012-04-16 08:55:02