[英]How to get the Drawable name from imageview in android and get it's String name for comparing
我有兩個蘋果繪制和如何在我的代碼,以獲得可繪制的名字,並比較它們與相同字母開頭
發問者實際上正在尋找2個問題-
首先,他想從imageview id獲取可繪制的id。 (在圖像視圖中顯示的thw drawable)
那么他想從步驟1中獲得的可繪制ID中獲得可繪制名稱。
更新的答案
沒有直接的方法。 您需要通過setTag()
將可繪制的ID保存在imageview的Tag
。 那么您可以通過getTag()
進行檢索。
您應該通過獲得圖像名稱
String imageName1 = context.getResources().getResourceEntryName(R.drawable.apple);
// now imageName1 should contain "apple".
String imageName2 = context.getResources().getResourceEntryName(R.drawable.a);
// now imageName2 should contain "a".
if(imageName1.toLowerCase().charAt(0) == imageName2.toLowerCase().charAt(0))
{
// starting character 'a' is matching
}
你有嘗試過嗎?
String name1 = getResources().getResourceEntryName(R.drawable.drawable_id_1);
String name2 = getResources().getResourceEntryName(R.drawable.drawable_id_2);
if (name1.startsWith("a") && name2.startsWith("a")) {
// do something
}
通過getResources().getResourceName(R.drawable.XXXX)
獲得完整的資源名稱。 這將以package:type/entry
的形式返回一個字符串。
例如,我的項目在我的資源中包含com.test.app
包和一個名為apple.png
的可繪制apple.png
。 調用我提到的方法的結果將是:
com.test.app:drawable/apple
當您要比較同一項目中的圖像時,可以輕松地截斷零件直到其實際名稱,因為所有圖像的名稱都相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.