如何在我的textInputLayout为空时显示错误。

huangapple go评论59阅读模式
英文:

How to put an error when my textInputLayout is empty

问题

button.setOnClickListener {
    val numberOne = textInputLayout.editText?.text.toString().toDouble()
    val numberTwo = textInputLayout2.editText?.text.toString().toDouble()
    val result = numberOne + numberTwo

    if (numberOne.isNaN()) {
        textInputLayout.error = "请输入数字"
    } else if (numberTwo.isNaN()) {
        textInputLayout2.error = "请输入数字"
    } else {
        textView.text = result.toString()
    }
}
<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>
英文:

I am carrying out addition of decimals with textInputLayout, the problem is that at the moment of calling isEmpty () to show an error in case the fields are empty, this option does not appear.

I want to do it this way

 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 = (&quot;enter number&quot;)
            }else
                if (numberTwo./*no appears isEmpty*/){
                    textInputLayout2.error = (&quot;enter number&quot;)
                }else {
                    textView.text = reult.toString()
                }
            
        }

xmlns

 &lt;com.google.android.material.textfield.TextInputLayout
        android:id=&quot;@+id/textInputLayout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;60dp&quot;
        android:hint=&quot;@string/numero&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:errorEnabled=&quot;true&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;

        &lt;com.google.android.material.textfield.TextInputEditText
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    &lt;/com.google.android.material.textfield.TextInputLayout&gt;

    &lt;com.google.android.material.textfield.TextInputLayout
        android:id=&quot;@+id/textInputLayout2&quot;
        style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;120dp&quot;
        android:hint=&quot;@string/numero2&quot;
        app:errorEnabled=&quot;true&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

        &lt;com.google.android.material.textfield.TextInputEditText
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    &lt;/com.google.android.material.textfield.TextInputLayout&gt;

答案1

得分: 3

你可以使用类似以下的代码片段:

<com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputEditText
        android:inputType="numberDecimal"
        .../>

</com.google.android.material.textfield.TextInputLayout>

你可以使用以下代码检查值:

val double: Double? = textInputLayout.editText?.text.toString().toDoubleOrNull()
val double2: Double? = textInputLayout2.editText?.text.toString().toDoubleOrNull()

if (double != null){
    // Double1 是一个数字
    textInputLayout.error = ""
    if (double2 != null){
        // Double2 是一个数字
        textInputLayout2.error = ""
        textview.text = (double + double2).toString()
    } else {
        // Double2 不是一个数字
        textInputLayout2.error = "错误"
        textview.text = ""
    }
} else {
    // Double1 不是一个数字
    textInputLayout.error = "错误"
    textview.text = ""
}

请注意,这里提供的只是代码的翻译,没有其他额外内容。

英文:

You can use something like:

        &lt;com.google.android.material.textfield.TextInputLayout&gt;

            &lt;com.google.android.material.textfield.TextInputEditText
                android:inputType=&quot;numberDecimal&quot;
                .../&gt;

And you can check the value with:

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 = &quot;&quot;
      if (double2 != null){
         //Double2 is a number
          textInputLayout2.error = &quot;&quot;
          textview.text = (double+double2).toString()
       } else {
          //Double2 is not a number
          textInputLayout2.error = &quot;Error&quot;
           textview.text = &quot;&quot;
       }
 } else {
     //Double1 is not a number
     textInputLayout.error = &quot;Error&quot;
     textview.text = &quot;&quot;
 }

答案2

得分: 0

numberOne 和 numberTwo 是双精度浮点数
val numberOne = textInputLayout.editText?.text.toString().toDouble()

isEmpty() 是一个字符串函数
尝试使用 numberOne.toString().isEmpty(),以及对 numberTwo 进行相同操作。

英文:

numberOne and numberTwo are double
val numberOne = textInputLayout.editText?.text.toString().toDouble()

isEmpty() is a string function
try numberOne.toString().isEmpty() and same for numberTwo

huangapple
  • 本文由 发表于 2020年9月1日 22:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63690245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定