一个活动中的多个和不同的计数器

时间:2016-10-30 14:40:52

标签: java android

基本上我需要一个包含多行的TableLayout,并且每行必须有一个不同的计数器(我可以通过按行中的两个按钮之一来加减1)。

在这里你可以看到一个明显的例子:

content_main.xml

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="55dp">

            <TextView
                android:text="Pizza Margherita"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView27"
                android:layout_weight="10"
                android:layout_column="1" />

            <Button
                android:text="-"
                android:layout_height="wrap_content"
                android:id="@+id/minusButton"
                android:layout_weight="1"
                android:layout_column="1"
                android:layout_width="50dp" />

            <TextView
                android:text="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/result"
                android:layout_weight="5"
                android:layout_column="1"
                android:textAlignment="center" />

            <Button
                android:text="+"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:id="@+id/plusButton"
                android:elevation="0dp"
                android:layout_weight="0.5"
                android:layout_column="1" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp">

            <TextView
                android:text="Pizza Salame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView27"
                android:layout_weight="10"
                android:layout_column="1" />

            <Button
                android:text="-"
                android:layout_height="wrap_content"
                android:id="@+id/minusButton2"
                android:layout_weight="1"
                android:layout_column="1"
                android:layout_width="50dp" />

            <TextView
                android:text="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/result2"
                android:layout_weight="5"
                android:layout_column="1"
                android:textAlignment="center" />

            <Button
                android:text="+"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:id="@+id/plusButton2"
                android:elevation="0dp"
                android:layout_weight="0.5"
                android:layout_column="1" />

        </TableRow>
</TableLayout>

我不知道如何使用 MainActivity.java 。 到目前为止,我试图实现一个数组(没有成功),并为每个计数器写一个单独的类(但结果太多了)。

任何提示?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,我建议您为计数器和适配器实现自定义视图逻辑,以使其更具可扩展性。

基本上你有:

  • counter_layout.xml - 声明组件
  • CounterView.java - 扩充布局并控制计数器的逻辑
  • CounterAdapter.java - 实例化CounterView的多个内容

然后,您可以在Activity中使用ListView来显示适配器的内容。

当然,您需要管理每个CounterView的状态。但这很容易,也许它可能是你项目的下一步。

相关问题