【问题标题】:error message displaying continuously in TextInputLayout在 TextInputLayout 中连续显示错误消息
【发布时间】:2016-02-29 15:36:32
【问题描述】:

此代码位于布局文件中。

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textInputLayoutMobile"
    >

    <EditText
        android:inputType="number"
        android:id="@+id/mobileNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="10"
        android:hint="Mobile Number" />

</android.support.design.widget.TextInputLayout>

这是 Java 代码。

TextInputLayout textInputLayoutMobile = (TextInputLayout)findViewById(R.id.textInputLayoutMobile);
textInputLayoutMobile.setErrorEnabled(true);
textInputLayoutMobile.setError("This field is required");

当前行为:当我们点击该字段时,它会显示“此字段为必填项”。此外,当我们开始在字段中输入时,此消息不会消失。

期望的行为:当我们第一次点击时,它不应显示“此字段为必填项”。当我们在触摸该字段后移动到另一个字段并且没有输入任何数据时,它应该显示“此字段是必需的”。此外,当我们开始在字段中输入时,此消息应该会消失。

【问题讨论】:

标签: android material-design


【解决方案1】:

我认为你必须使用文本更改监听器

edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
             //set error
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

和他的方法来设置你的愿望输出..希望对你有所帮助!

【讨论】:

    【解决方案2】:

    我认为错误是在从您的布局中检索textInputLayoutMobile 时使用了这两行代码(可能在onCreate 中)。所以view被设置为总是显示这个错误信息。

    textInputLayoutMobile.setErrorEnabled(true);
    textInputLayoutMobile.setError("This field is required");
    

    我想这两行应该从这一点移动并设置在那里你应该验证textInputLayoutMobile 输入并确定验证是错误的,例如在onClick 回调或侦听器中。此外,当您的输入验证为 true 时,请设置 textInputLayoutMobile.setErrorEnabled(false);

    在此处查看示例:http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168

    【讨论】:

      【解决方案3】:

      我认为您需要使用 id="@+id/mobileNumber",设置小部件,然后尝试类似的操作

      your_widget.setOnClickListener(new OnClickListener() {

              public void onClick(final View v) {
                  if (yourwidget.getText().toString().length() == 0) {
                      yourwidget.setError("This field is required");}
      

      其他...

      您可以在“下一步”按钮或类似的东西(保存按钮等)上使用此控件,直接在现场使用(对不起,我的英语不好)

      【讨论】:

        【解决方案4】:

        我认为您需要参考 EditText 而不是 TextInputLayout。 试试

         EditText editText = (EditText)findViewById(R.id.mobileNumber);
         editText.setError("This field is required");
        

        【讨论】:

        • 你确定 EditText 有方法 .setErrorEnabled(true); ?
        猜你喜欢
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        • 2016-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        相关资源
        最近更新 更多