在App(Kotlin)的同一页面上使用TextView创建多组加号和减号按钮

问题描述 投票:0回答:1

我有两个控件,可以增加或减少一个整数。我单击第一组或第二组加号和减号按钮,尽管它们是独立的功能,但它将在两个输出上都显示整数。例如,如果我单击integer_number_1的加号和减号按钮,它将在integer_number_1和integer_number_2输出/ TextView上同时显示数字。

我该如何解决???我有很多不同的方式,但没有一种有效。

“截图”

这是我的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"
       tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:layout_marginBottom="549dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/decrease_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />

        <TextView
            android:id="@+id/integer_number_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="40dp"
            android:layout_marginBottom="16dp"
            android:inputType="number"
            android:text="0"
            android:textSize="70sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/increase_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />


    </LinearLayout>

    <Button
        android:id="@+id/decrease_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/integer_number_2"
        app:layout_constraintHorizontal_bias="0.513"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.317" />

    <Button
        android:id="@+id/increase_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.579"
        app:layout_constraintStart_toEndOf="@+id/integer_number_2"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.313" />

    <TextView
        android:id="@+id/integer_number_2"
        android:layout_width="46dp"
        android:layout_height="99dp"
        android:layout_marginStart="46dp"
        android:layout_marginLeft="46dp"
        android:inputType="number"
        android:text="0"
        android:textSize="70sp"
        android:textStyle="bold"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.426"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.297" />


</androidx.constraintlayout.widget.ConstraintLayout>

这是我的kt文件:

package com.example.plus_and_minus_input

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    increase_1.setOnClickListener { increaseInteger1() }
    decrease_1.setOnClickListener { decreaseInteger1() }


    increase_2.setOnClickListener { increaseInteger2() }
    decrease_2.setOnClickListener { decreaseInteger2() }


}

fun increaseInteger1() {
    display(integer_number_1.text.toString().toInt() + 1)
}

fun decreaseInteger1() {
    display(integer_number_1.text.toString().toInt() - 1)
}


fun increaseInteger2() {
    display(integer_number_2.text.toString().toInt() + 1)
}

fun decreaseInteger2() {
    display(integer_number_2.text.toString().toInt() - 1)
}

private fun display(number: Int) {
    integer_number_1.setText("$number")
    integer_number_2.setText("$number")
}

}
android kotlin input output
1个回答
0
投票

我认为您需要将以下显示方法分开。

private fun display(number: Int) {
    integer_number_1.setText("$number")
    integer_number_2.setText("$number")
}

to

private fun display_number_1(number: Int) {
    integer_number_1.setText("$number")
}

private fun display_number_2(number: Int) {
    integer_number_2.setText("$number")
}
© www.soinside.com 2019 - 2024. All rights reserved.