在 Android Studio (Kotlin) 中设置增量和减量的限制

Set limits for increments and decrements in Android Studio (Kotlin)

所以我在单个页面应用程序上有 2 组递增和递减按钮。我想为减量设置 0 的限制,为增量设置 10 的限制。我尝试了一些不同的 "if" 语句,一个与 decrease_1.setOnClickListener 结合,另一个与有趣的 increaseInteger1() 结合,但没有任何效果。我做错了什么?

class MainActivity : AppCompatActivity() {

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

    increase_1.setOnClickListener { increaseInteger1() }
    decrease_1.setOnClickListener { decreaseInteger1()
        if(integer_number_1.text.length < 0) {
            integer_number_1.text.length === 0
        }
    }



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


}

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

    /*val stringNumber = integer_number_1.text.toString()
    val intNumber = stringNumber.toInt()
    val increasedNumber = intNumber + 1

    if(increasedNumber < 0) { display_number_1(0) }
    else if (increasedNumber > 10) {display_number_1(10)} }*/
}

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



}


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

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



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

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

} 

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>

=== 是恒等运算符。您应该使用赋值运算符 = 来更改值。

使用 TextView 中的字符串作为存储整数值的方法非常笨拙。使用 属性 作为值会更简洁。 (在更复杂的情况下,您会将值放在 ViewModel 的 LiveData 中。)

您可以使用强制函数来限制您的值。

将其包装在一起:

private var integer1 = 0
private var integer2 = 0

fun increaseInteger1() {
    integer1 = (integer1 + 1).coerceAtMost(10)
    display_number_1(integer1)
}

fun decreaseInteger1() {
    integer1 = (integer1 - 1).coerceAtLeast(0)
    display_number_1(integer1)
}

//...

您也可以使用 min/max 而不是强制函数:

integer1 = min(10, integer1 + 1) // increment
integer1 = max(0, integer1 - 1) // decrement

或者您可以为属性提供自定义 setter,强制它们保持在您想要的限制范围内:

private var integer1: Int = 0
    set(value) { field = value.coerceIn(0..10) }
private var integer2: Int = 0
    set(value) { field = value.coerceIn(0..10) }

//...
fun increaseInteger1() {
    display_number_1(++integer1)
}

fun decreaseInteger1() {
    display_number_1(--integer1)
}