如何在android中使用元素标记找到xml元素

Ara*_*hna 3 android android-layout

我在xml中有一个包含许多行和列的表格布局.我已经为表格的每一列提供了一个特定的标签.

现在我想根据某些条件访问特定列.如何让标签与字符串匹配,以便我可以访问该列?

例如,我的xml代码是这样的

<TableLayout>

        <TableRow
            android:padding="10dp"
            android:id="@+id/row1"
            android:layout_height="wrap_content"
            android:weightSum="100"
            android:background="#ffffff"
            android:layout_margin="1dp"
            android:layout_weight="16.66">

            <TextView
                android:text="Day"
                android:textStyle="bold"
                android:clickable="true"             
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="11.11" />


            <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym1"
            android:tag="1"
            android:text="9:30" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym2"
            android:tag="2"
            android:text="10:30" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym3"
            android:tag="3"
            android:text="11:30" />

         <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym4"
            android:tag="4"
            android:text="12:30" />

          <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym5"
            android:tag="5"
            android:text="1:30" />

           <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym6"
            android:tag="6"
            android:text="2:30" />

           <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym7"
            android:tag="7"
            android:text="3:30" />

           <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="11.11"
            android:id="@+id/tym8"
            android:tag="8"
            android:text="4:30" />
        </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,每列都有一个标记.在我的代码中,当用户输入标记值时,我想访问与标记值匹配的列.

例如在java类中 -

String test="4";
TextView column_var;
Run Code Online (Sandbox Code Playgroud)

现在我想将column_var设置为xml中与测试字符串具有相同标记的TextView列.

怎么实现呢?请帮忙!!!

Rob*_* H. 5

您可以使用findViewWithTag()找到具有特定标记的视图

但是你应该使用ID,因为:

通常最好使用ID(通过android:id属性)而不是标签,因为它们更快并允许编译时类型检查.

编辑:方法findViewWithTag()是View类的成员方法.你必须找到像这样的parrent视图

View parrentView = findViewById( R.id.row1 );
Run Code Online (Sandbox Code Playgroud)

然后你可以找到你的textview

TextView column_var = (TextView) parrentView.findViewWithTag(test);
Run Code Online (Sandbox Code Playgroud)