Comment connaître tous les identifiants que j'ai dans le fichier main.xml dans Android?

StackOverflow https://stackoverflow.com/questions/2583591

  •  24-09-2019
  •  | 
  •  

Question

Je suis nouveau chez Android World, et j'ai un doute, y a-t-il une méthode qui me donne le nom des identifiants que je crée dans main.xml? Par exemple, j'ai ceci:

main.xml

<TextView android:id="@+id/text1"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="Google"
       />

<TextView android:id="@+id/text2"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="As"
       />

Et ce que je veux, c'est le nom d'identification des deux textes, y a-t-il une méthode que je peux utiliser dans ma classe .java qui me donne pour cet exemple l'identifiant? Dans ce cas, je veux le (text1 et text2).

Merci et pardonnez l'anglais.

Était-ce utile?

La solution

Réglez un identifiant sur la disposition des parents et essayez quelque chose comme ceci:

    LinearLayout ll = findViewById(R.id.yourLayout);
    for(int i=0; i<ll.getChildCount(); i++){
        View v = ll.getChildAt(i);
        int idView = v.getId();
            //Do something with idView
    }

Autres conseils

gonfler le fichier

linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);

Ensuite, exécutez FindViewByid pour transformer TextView en un objet

TextView text1 = (TextView)main.findViewById(R.id.text1);

Ensuite, vous pouvez travailler avec l'objet Text1 plus tard comme celui-ci

text1.setText("foo");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top