Java 如何获取android中给定xml文件中的组件列表及其id?

Java 如何获取android中给定xml文件中的组件列表及其id?,java,android,xml,Java,Android,Xml,。我是android的新手。对于某些需要,我需要在布局xml文件中获取所有组件(即文本视图、按钮、编辑文本、图像视图、微调器、复选框等)及其ID。 假设我有以下xml文件sample.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_

。我是android的新手。对于某些需要,我需要在布局xml文件中获取所有组件(即文本视图、按钮、编辑文本、图像视图、微调器、复选框等)及其ID。 假设我有以下xml文件
sample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>



</LinearLayout>

目前我只有xml文件的id。我需要获取所有组件和id,以便使用java代码修改它们的文本。简而言之,我需要某种解析。 假设我在xml文件中除了id之外什么都不知道

所以请帮帮我。先谢谢你


实际上,我需要更改布局中每个文本视图的文本,比如说,我需要在每个文本视图的文本末尾添加*ath。

好吧,您没有办法实现您想要的

如果所有ID都是在xml布局文件中创建和定义的——您必须在代码中调用它们——但您必须知道它们的给定ID

如果您通过代码创建ID,例如
textView.setId(1234)-然后您可以跟踪所有名称并对其执行任何操作


因此,不幸的是,我认为您想要的不可能。

您尝试过findViewById吗?@Nitish,我不知道xml文件中每个元素的ID。findViewById()只有在我知道我的id时才起作用。是否动态创建视图?否。它是使用xml代码创建的。创建布局时@SACHIN-782我认为这是不可能的,除非您仅通过代码创建xml!