将 TextView 中的整数重置为 0 (Kotlin)
Reset integers in a TextView to 0 (Kotlin)
所以我可以使用递增和递减按钮,但我需要使用清除按钮将第二组重置为 0。如果你按下它,它会显示一个 0 作为字符串值,但它不会重置输出。例如,如果我要将整数值递增到 6,然后按下清除按钮,它会在 TextView 中显示 0,但是一旦我再次按下递增按钮,它会直接上升到 7 而不是 1。
我错过了什么?
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() }
clear_button.setOnClickListener {
integer_number_2.text ="0"
}
}
private var integer1 = 0
private var integer2 = 0
fun increaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() + 1)
integer1 = (integer1 + 1).coerceAtMost(10)
display_number_1(integer1)
}
fun decreaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() - 1)
integer1 = (integer1 - 1).coerceAtLeast(0)
display_number_1(integer1)
}
fun increaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() + 1)
integer2 = (integer2 + 1).coerceAtMost(10)
display_number_2(integer2)
}
fun decreaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() - 1)
integer2 = (integer2 - 1).coerceAtLeast(0)
display_number_2(integer2)
}
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="309dp"
android:layout_height="118dp"
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" />
<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/integer_number_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
您还需要重置全局 int var 的值:
clear_button.setOnClickListener {
integer_number_2.text ="0"
integer2 = 0
}
在 kotlin/java 中用下划线命名变量是不好的做法。使用驼峰式大小写代替 tvIntegerNumberTwo
并可能为其分配 xml 元素类型的首字母以更好地识别它是什么。
所以我可以使用递增和递减按钮,但我需要使用清除按钮将第二组重置为 0。如果你按下它,它会显示一个 0 作为字符串值,但它不会重置输出。例如,如果我要将整数值递增到 6,然后按下清除按钮,它会在 TextView 中显示 0,但是一旦我再次按下递增按钮,它会直接上升到 7 而不是 1。
我错过了什么?
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() }
clear_button.setOnClickListener {
integer_number_2.text ="0"
}
}
private var integer1 = 0
private var integer2 = 0
fun increaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() + 1)
integer1 = (integer1 + 1).coerceAtMost(10)
display_number_1(integer1)
}
fun decreaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() - 1)
integer1 = (integer1 - 1).coerceAtLeast(0)
display_number_1(integer1)
}
fun increaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() + 1)
integer2 = (integer2 + 1).coerceAtMost(10)
display_number_2(integer2)
}
fun decreaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() - 1)
integer2 = (integer2 - 1).coerceAtLeast(0)
display_number_2(integer2)
}
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="309dp"
android:layout_height="118dp"
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" />
<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/integer_number_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
您还需要重置全局 int var 的值:
clear_button.setOnClickListener {
integer_number_2.text ="0"
integer2 = 0
}
在 kotlin/java 中用下划线命名变量是不好的做法。使用驼峰式大小写代替 tvIntegerNumberTwo
并可能为其分配 xml 元素类型的首字母以更好地识别它是什么。