我有多个项目,每个项目都有一个textview,指定用户选择的项目数。当用户单击按钮时,必须更改相应的文本视图以显示更新的数字。除了为每个项目的按钮和textview提供单独的ID并为它们编写代码之外,还有其他方法吗?
这是一个xml文件,有助于理解我对android编程的新认识,任何建议或帮助对我都有很大的帮助。感谢。
<TableLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TableRow>
<LinearLayout android:layout_height="200dp" android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/abc"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp"
android:text="ABC"/>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal">
<TextView
android:id="@+id/itemsNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Items : 0"/>
<Button
android:id="@+id/plusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"/>
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:layout_height="300dp" android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/xyz"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="XYZ"/>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal">
<TextView
android:id="@id/itemsNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Items : 0"/>
<Button
android:id="@id/plusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"/>
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:layout_height="300dp" android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/ijk"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="IJK"/>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal">
<TextView
android:id="@id/itemsNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Items : 0"/>
<Button
android:id="@id/plusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"/>
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:layout_height="300dp" android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/abc1"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="ABC1"/>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal">
<TextView
android:id="@id/itemsNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Items : 0"/>
<Button
android:id="@id/plusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"/>
</LinearLayout>
</TableRow>
</TableLayout>
答案 0 :(得分:0)
如果它们始终是相同的布局,则应创建一个扩展View
的自定义窗口小部件,其中包含TextView
和Button
。如果您永远不需要从代码中引用它们,那么在执行一次之后就不需要编写任何内容。如果确实需要从代码中引用它,则必须在自定义视图窗口小部件上设置id才能引用它。