リソースIDからリソース名を取得する方法 質問する

リソースIDからリソース名を取得する方法 質問する


私のレイアウトでは、次のように定義しました。

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dnt want this text" />

アクティビティ内の何らかの関数がこのID(ラジオボタンのID)を返すと仮定します。次に、このテキストを取得します。ラジオ1このIDからテキストを取得したいのですラジオ1で書かれているandroid:id="@+id/radio1"

どうすればそれが可能なのか誰か教えてもらえますか?

ベストアンサー1

アクティビティで次のことを試してください:

  1. 次のような文字列を取得しますradio1:

    getResources().getResourceEntryName(int resid);
    
  2. 次のような文字列を取得しますcom.sample.app:id/radio1:

    getResources().getResourceName(int resid);
    

Kotlin の現在:

val name = v.context.resources.getResourceEntryName(v.id)

おすすめ記事