当我的 textInputLayout 为空时如何出错
Posted
技术标签:
【中文标题】当我的 textInputLayout 为空时如何出错【英文标题】:How to put an error when my textInputLayout is empty 【发布时间】:2020-12-20 17:17:37 【问题描述】:我正在用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_
android:layout_
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_
android:layout_ />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_
android:layout_
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_
android:layout_ />
</com.google.android.material.textfield.TextInputLayout>
【问题讨论】:
【参考方案1】:你可以使用类似的东西:
<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 = ""
【讨论】:
准备好反映错误但我不知道如何添加数字并显示结果 把你的逻辑移到//Number is double
我这样做了:val double: Double? = number1.editText?.text.toString().toDoubleOrNull() double?.let // Number is double... ?: (number1.setError("Enter number") ) val double2: Double? = number1.editText?.text.toString().toDoubleOrNull() double2?.let // Number is double... ?: (number2.setError("Enter number") )
我是这样做的,所以错误出现在两个字段中,对不起我是初学者
@JorgeLeonardo 以更简单的方式重写了答案【参考方案2】:
numberOne 和 numberTwo 是双精度 val numberOne = textInputLayout.editText?.text.toString().toDouble()
isEmpty() 是一个字符串函数 尝试 numberOne.toString().isEmpty() 和 numberTwo 相同
【讨论】:
好的,那么您可以尝试将 num1,num2 声明为字符串,然后将结果转换为双精度值,例如 val numberOne = textInputLayout.editText?.text.toString() val numberTwo = textInputLayout2.editText?。 text.toString() val reult = numberOne.toDouble() + numberTwo.toDouble();以上是关于当我的 textInputLayout 为空时如何出错的主要内容,如果未能解决你的问题,请参考以下文章
当我说可以为空或为空时,我不断收到“db context null”,我该如何解决?
当我的搜索字段为空时,如何在不搜索结果的情况下呈现页面,但在搜索时仍然异步呈现?