如何知道所有id';我在android的main.xml文件中有什么?

如何知道所有id';我在android的main.xml文件中有什么?,android,textview,Android,Textview,我是Android world的新手,我有一个疑问,是否有任何方法可以给我在main.xml中创建的id的名称? 例如,我有: main.xml <TextView android:id="@+id/text1" android:layout_width="70px" android:layout_height="70px" android:text="Google" /> <TextView android:id="@

我是Android world的新手,我有一个疑问,是否有任何方法可以给我在main.xml中创建的id的名称? 例如,我有:

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

我想要的是来自two-TextView的id名称,在我的class.java中是否有任何方法可以为我提供这个示例的id?在这种情况下,我需要(text1和text2)

谢谢并原谅我的英语。

把文件放大

linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);
然后运行findViewById将TextView转换为对象

TextView text1 = (TextView)main.findViewById(R.id.text1);
之后,您可以像这样使用对象text1

text1.setText("foo");
把锉刀充气

linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);
然后运行findViewById将TextView转换为对象

TextView text1 = (TextView)main.findViewById(R.id.text1);
之后,您可以像这样使用对象text1

text1.setText("foo");

将id设置为父布局,然后尝试以下操作:

    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
    }
LinearLayout ll=findViewById(R.id.yourLayout);

对于(int i=0;i将id设置为父布局,然后尝试以下操作:

    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
    }
LinearLayout ll=findViewById(R.id.yourLayout);

对于(inti=0;iHi,谢谢。但我想要的是更动态的,我知道如果我知道id的名称,我可以使用以下命令获得:TextView text1=(TextView)findViewById(R.id.text1);但是,假设我不知道id的名称,如果有任何方法提供main.xml中id的所有名称,我想要什么。然后创建对象,一个静态的东西是所有的都是TextView。我想要这个,例如:name_offids=methodgivemeallidsname;然后创建Objects TextView text1=(TextView)findViewById(R.id.name\u officeds);有没有任何方法可以给我id的名称?谢谢,谢谢。但是我想要的是更动态的,我知道如果我知道id的名称,我可以使用这个:TextView text1=(TextView)findViewById(R.id.text1);但是,假设我不知道id的名称,如果有任何方法提供main.xml中id的所有名称,我想要什么。然后创建对象,一个静态的东西是所有的都是TextView。我想要这个,例如:name_offids=methodgivemeallidsname;然后创建Objects TextView text1=(TextView)findViewById(R.id.name\u officeds);是否有任何方法可以给我id的名称?谢谢谢谢谢谢谢谢你是我想要的东西。谢谢你是我想要的东西。