【问题标题】:How do I update the Quantity when user press "+" or "-" button?用户按“+”或“-”按钮时如何更新数量?
【发布时间】:2021-10-04 11:05:08
【问题描述】:

注意:这必须在 Kotlin 中完成

我想在用户按下“+”或“-”按钮时更新数量 enter image description here

我还希望“0”(数量)在两个按钮之间对齐。

<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>

这是 Kotlin 文件代码 (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
    }
}

【问题讨论】:

    标签: android android-studio kotlin android-layout kotlin-android-extensions


    【解决方案1】:

    首先在你的 main 方法上方创建一个计数器:

    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
    }
    

    【讨论】:

      【解决方案2】:

      我会给你一步一步的接收,而不是在这里写整个代码。

      第 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

      【讨论】:

        【解决方案3】:

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

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

        然后解析成整数

        int quantity = Integer.parseInt(currentQuant);
        

        现在您拥有int 的当前数量,只需增加它,就像这样

        quantity ++ ;
        

        然后

        qtr.settext(String.valueOf(quantity));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 2022-07-27
          • 1970-01-01
          • 1970-01-01
          • 2022-11-29
          • 2019-10-22
          • 1970-01-01
          相关资源
          最近更新 更多