¿Cómo saber todas las identificaciones que tengo en el archivo main.xml en Android?

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

  •  24-09-2019
  •  | 
  •  

Pregunta

Soy nuevo en Android World, y tengo dudas, ¿hay algún método que me dé el nombre de las identificaciones que creo en main.xml? Por ejemplo, tengo esto:

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"
       />

Y lo que quiero es el nombre de identificación de los dos TextView, ¿hay algún método que pueda usar en mi clase .java que me dé para este ejemplo la identificación? En este caso quiero el (Text1 y Text2).

Gracias y perdona mi inglés.

¿Fue útil?

Solución

Establezca una identificación en el diseño de los padres y pruebe algo como esto:

    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
    }

Otros consejos

inflar el archivo

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

Luego ejecute FindViewByID para convertir TextView en un objeto

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

Entonces puedes trabajar con Object Text1 más tarde como este

text1.setText("foo");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top