因此,我在单个页面应用程序上有两组增量和减量按钮。我想将限制设置为0(递减)和10(增量)。我尝试了许多不同的“ if”语句,其中一个与reduce_1.setOnClickListener结合使用,另一个与有趣的increasInteger1()结合使用,但没有任何效果。我究竟做错了什么?
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
或者,您可以为属性提供一个自定义设置器,以强制它们保持在所需的范围内:
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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句