1

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

 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
        }
     } 
  }
4

4 回答 4

3

试试这个,

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() 产生不可见的错误文本

于 2013-03-12T13:04:36.720 回答
1

使用 Html 对象进行简单快速的格式化:

this.txtUserName.setError(Html.fromHtml("<font color='red'>Incorrect input!</font>"));
于 2013-12-18T09:58:38.670 回答
0

试试这个代码:

               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 );

                }
于 2013-03-12T13:09:41.963 回答
0

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

更改前:

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

更改后:

android:theme="@android:style/Theme.Black.NoTitleBar"
于 2013-04-25T08:40:40.000 回答