-1

Note: This has to be done in Kotlin

I want to update the quantity when the user presses the "+" or the "-" button enter image description here

I also want the "0" (Quantity) to be aligned between the two buttons.

<RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginStart="36dp"
   android:layout_marginTop="36dp"
   android:orientation="vertical"
   tools:ignore="HardcodedText">

   <TextView
       android:id="@+id/Quantity"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="16dp"
       android:text="QUANTITY"
       android:textSize="23sp" />

   <TextView
       android:id="@+id/qtr"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_toRightOf="@+id/add"
       android:text="@string/Qtr"
       android:textSize="23sp" />

   <Button
       android:id="@+id/orderbutton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/add"
       android:onClick="order"
       android:text="Order" />

   <Button
       android:id="@+id/add"
       android:layout_width="45dp"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_marginRight="16dp"
       android:text="+" />

   <Button
       android:id="@+id/sub"
       android:layout_width="45dp"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_marginLeft="16dp"
       android:layout_toRightOf="@+id/qtr"
       android:text="-" />

</RelativeLayout>

And this is the Kotlin file code (MainActivity.kt)

package com.example.demoapplicaltion
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Write code here
    }
}
4

3 回答 3

2

首先在您的主要方法上方创建一个计数器:

var quantity: Int = 0

接下来将点击侦听器设置为 onCreate() 中的两个按钮:

findViewById<Button>(R.id.add).setOnClickListener{
  quantity++
  updateValue()
}
findViewById<Button>(R.id.min).setOnClickListener{
  quantity--
  updateValue()
}

最后创建 updateValue() 方法:

fun updateValue(){
  findViewById<TextView>(R.id.Quantity).text = quantity
}
于 2021-10-04T11:17:05.627 回答
0

我会一步一步地给你recepy,而不是在这里写整个代码。

第 1 步 在您的 MainActivity 中,您需要对两个按钮和 xml 中的 textview 进行某种引用,以便对它们执行任何操作。最简单的方法是使用 findViewById() 函数。例如,您需要一个变量,例如

var addButton: Button? = null

在您的活动中。

第 2 步 将 xml 中的按钮分配给此变量调用

addButton = findViewById( R.id.add )

第 3 步 现在,来自 xml 的按钮已分配给活动中的变量,您可以访问它来定义单击按钮时应该发生的情况。例如:

addButton.setOnClickListener {
code that increases the quantity, sth like:
val oldQuantity = quantityTextView.text.toInt()
quantityTextView.text = oldQuantity + 1
}

要查看 findViewById 工作,请查看:https ://www.tutorialkart.com/kotlin-android/access-a-view-programmatically-using-findviewbyid/ 请 注意,有更好但更复杂的链接方式xml和Activity之类的ViewBinding:https ://developer.android.com/topic/libraries/view-binding

于 2021-10-04T11:32:40.607 回答
0

首先从显示中获取当前数量textview,像这样

String currentQuant = qtr.gettext().toString();

然后解析成整数

int quantity = Integer.parseInt(currentQuant);

现在你有当前的数量int只是增加它,像这样

quantity ++ ;

然后

qtr.settext(String.valueOf(quantity));
于 2021-10-04T11:14:35.740 回答