当我的 textInputLayout 为空时如何出错
How to put an error when my textInputLayout is empty
我是用textInputLayout进行小数加法,问题是在调用isEmpty()时如果字段为空报错,这个选项没有出现
我想这样做
button.setOnClickListener {
val numberOne = textInputLayout.editText?.text.toString().toDouble()
val numberTwo = textInputLayout2.editText?.text.toString().toDouble()
val reult = numberOne + numberTwo
if (numberOne./*no appears isEmpty*/){
textInputLayout.error = ("enter number")
}else
if (numberTwo./*no appears isEmpty*/){
textInputLayout2.error = ("enter number")
}else {
textView.text = reult.toString()
}
}
xmlns
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:hint="@string/numero"
app:layout_constraintEnd_toEndOf="parent"
app:errorEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:hint="@string/numero2"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
numberOne 和 numberTwo 是 double
val numberOne = textInputLayout.editText?.text.toString().toDouble()
isEmpty() 是一个字符串函数
尝试 numberOne.toString().isEmpty() 和相同的 numberTwo
您可以使用类似的东西:
<com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText
android:inputType="numberDecimal"
.../>
您可以使用以下方法检查值:
val double: Double? = textInputLayout.editText?.text.toString().toDoubleOrNull()
val double2: Double? = textInputLayout2.editText?.text.toString().toDoubleOrNull()
if (double != null){
//Double1 is a number
textInputLayout.error = ""
if (double2 != null){
//Double2 is a number
textInputLayout2.error = ""
textview.text = (double+double2).toString()
} else {
//Double2 is not a number
textInputLayout2.error = "Error"
textview.text = ""
}
} else {
//Double1 is not a number
textInputLayout.error = "Error"
textview.text = ""
}
我是用textInputLayout进行小数加法,问题是在调用isEmpty()时如果字段为空报错,这个选项没有出现
我想这样做
button.setOnClickListener {
val numberOne = textInputLayout.editText?.text.toString().toDouble()
val numberTwo = textInputLayout2.editText?.text.toString().toDouble()
val reult = numberOne + numberTwo
if (numberOne./*no appears isEmpty*/){
textInputLayout.error = ("enter number")
}else
if (numberTwo./*no appears isEmpty*/){
textInputLayout2.error = ("enter number")
}else {
textView.text = reult.toString()
}
}
xmlns
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:hint="@string/numero"
app:layout_constraintEnd_toEndOf="parent"
app:errorEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:hint="@string/numero2"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
numberOne 和 numberTwo 是 double
val numberOne = textInputLayout.editText?.text.toString().toDouble()
isEmpty() 是一个字符串函数 尝试 numberOne.toString().isEmpty() 和相同的 numberTwo
您可以使用类似的东西:
<com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText
android:inputType="numberDecimal"
.../>
您可以使用以下方法检查值:
val double: Double? = textInputLayout.editText?.text.toString().toDoubleOrNull()
val double2: Double? = textInputLayout2.editText?.text.toString().toDoubleOrNull()
if (double != null){
//Double1 is a number
textInputLayout.error = ""
if (double2 != null){
//Double2 is a number
textInputLayout2.error = ""
textview.text = (double+double2).toString()
} else {
//Double2 is not a number
textInputLayout2.error = "Error"
textview.text = ""
}
} else {
//Double1 is not a number
textInputLayout.error = "Error"
textview.text = ""
}