【问题标题】:Android setError() remains blankAndroid setError() 保持空白
【发布时间】:2013-03-12 13:01:06
【问题描述】:

我有这几行代码来验证表单输入。但是,它不输出任何东西 - 只是一个空白框。请问我错过了什么?

 public void onClick(View view)
 {
    if( view.getId() == R.id.btnLogin )
    {
        TestClass.setUsername( txtUserName.getEditableText().toString() );
        TestClass.setPassword( txtPassword.getEditableText().toString() );

        if( TestClass.getUsername().toString().length() == 0 )
        {
            this.txtUserName.setError( "Incorrect input!" );
        }
        else if( TestClass.getPassword().toString().length() == 0 )
        {
         this.txtPassword.setError( "Incorrect input!" );
        }
        else
        {
               // do the login stuff here
        }
     } 
  }

【问题讨论】:

标签: android validation


【解决方案1】:

试试这个,

int ecolor = Color.RED; // whatever color you want
String estring = "Input is incorrect";  // your error message
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
myedittext.setError(ssbuilder);

来源:Android EditText.setError() yields invisible error text

【讨论】:

    【解决方案2】:

    使用 Html 对象轻松快速地格式化:

    this.txtUserName.setError(Html.fromHtml("<font color='red'>Incorrect input!</font>"));
    

    【讨论】:

      【解决方案3】:

      试试这个代码:

                     if (editTextName.getText().toString().length() == 0) {
                          int ecolor = getResources().getColor(R.color.blue); // whatever color you want
                          String estring = "Input is incorrect";
                          ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                          SpannableStringBuilder builder = new SpannableStringBuilder(estring);
                          ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                          editTextName.setError( builder );
      
                      }
      

      【讨论】:

      • 对不起,我在发帖前搜索了一遍,但可能不够详细。
      【解决方案4】:

      我刚刚在我的项目中更改了应用程序主题“清单文件和错误文本现在出现了。

      更改前:

      android:theme="@android:style/Theme.Light.NoTitleBar"
      

      更改后:

      android:theme="@android:style/Theme.Black.NoTitleBar"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多