adsl9002 2022-09-05 21:23 采纳率: 46.2%
浏览 24
已结题

想把按键监听的数值整合一起显示在textview

点击确认后,没法实现数值整合的效果,看日志都是给后面的数值覆盖了,想要这些数值整合一起显示textview里

img

MailActivity,kt:

package android.example.drink

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), View.OnClickListener {

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       /*   val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
          recyclerView.layoutManager = LinearLayoutManager(this)
          recyclerView.adapter = DrinkAdapter(drinkList)  */
       val okBtn: Button = findViewById(R.id.okBtn)

       okBtn.setOnClickListener(this)

   }

   fun drink(
       name1: String = "",
       name2: String = "",
       name3: String = "",
       name4: String = "",
       quantity: Int = 0,
       unitPrice: Int = 0
   ) {
       val message = name1 +   name2 +   name3 +   name4 +   quantity +   unitPrice
       Log.d("MailActivity", message)
       val xinXiView: TextView = findViewById(R.id.xiaoPiAo)
       xinXiView.text = message

   }

   override fun onClick(v: View?) {
       val okBtn: Button = findViewById(R.id.okBtn)
       val raGroup: RadioGroup = findViewById(R.id.RaGroup)
       val keleRtn: RadioButton = findViewById(R.id.keLe)
       val milkRtn: RadioButton = findViewById((R.id.milkTea))
       val dianCan: EditText = findViewById(R.id.jiBei)
       val iceBox: CheckBox = findViewById(R.id.bingKuAi)
       val zhenZhuBox: CheckBox = findViewById(R.id.zhenZhu)
       val jsBtn: Button = findViewById(R.id.jieSuAn)

       when (v) {
           okBtn -> {
               if (keleRtn.isChecked) {
                   drink("可乐", unitPrice = 8)
               }
               if (milkRtn.isChecked) {
                   drink(name2 = "奶茶", unitPrice = 12)
               }
               if (iceBox.isChecked) {
                   drink(name3 = "+冰")
               }
               if (zhenZhuBox.isChecked) {
                   drink(name4 = "+珍珠")
               }
               if (dianCan.text.isNotEmpty()) {
                   val int = Integer.parseInt(dianCan.text.toString())
                   drink(quantity = int)
               }
               dianCan.clearFocus()
               /*    raGroup.clearCheck()  */
               dianCan.setText("")
           }
       }
   }
}

activity_mail.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.recyclerview.widget.RecyclerView>

    <TextView
        android:id="@+id/xiaoPiAo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:padding="16dp"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/RaGroup"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerView"
        app:layout_constraintVertical_bias="0.0" />

    <RadioGroup
        android:id="@+id/RaGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/bingKuAi"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <RadioButton
            android:id="@+id/keLe"
            android:layout_width="68dp"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="@string/kele"
            android:textColor="@color/black"
            tools:ignore="TextContrastCheck" />

        <RadioButton
            android:id="@+id/milkTea"
            android:layout_width="68dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:gravity="center_vertical"
            android:text="@string/milkTea"
            android:textColor="@color/black"
            tools:ignore="TextContrastCheck" />
    </RadioGroup>

    <EditText
        android:id="@+id/jiBei"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/beiShu"
        android:inputType="number"
        android:minHeight="48dp"
        android:textColorHint="#616161"
        app:layout_constraintBottom_toTopOf="@+id/jieSuAn"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/jieSuAn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="50dp"
        android:backgroundTint="#686868"
        android:text="@string/jieSuAn"
        android:textColor="#E8EAF6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="TextContrastCheck" />

    <Button
        android:id="@+id/okBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="116dp"
        android:backgroundTint="#686868"
        android:text="@string/ok"
        android:textColor="#E8EAF6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/jiBei"
        tools:ignore="TextContrastCheck" />

    <CheckBox
        android:id="@+id/bingKuAi"
        android:layout_width="68dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/bing"
        android:textColor="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/jiBei"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="TextContrastCheck" />

    <CheckBox
        android:id="@+id/zhenZhu"
        android:layout_width="68dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/zhenZhu"
        android:textColor="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/jiBei"
        app:layout_constraintStart_toEndOf="@+id/bingKuAi"
        tools:ignore="TextContrastCheck" />


</androidx.constraintlayout.widget.ConstraintLayout>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 9月9日
    • 创建了问题 9月5日

    悬赏问题

    • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
    • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错