我有几行代码来验证表单输入。但是,它不输出任何东西 - 只是一个空白框。我想念的是什么?
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
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:1)
使用Html对象轻松快速地格式化:
this.txtUserName.setError(Html.fromHtml("<font color='red'>Incorrect input!</font>"));
答案 2 :(得分: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 );
}
答案 3 :(得分:0)
我刚刚更改了项目中的应用程序主题“清单文件,现在出现错误文本。
改变之前:
android:theme="@android:style/Theme.Light.NoTitleBar"
更改后:
android:theme="@android:style/Theme.Black.NoTitleBar"