2

如何在 android 中这样的文本字段中实现警告:

4

4 回答 4

3

只需使用 TextView.setError 方法。

textView.setError("Field can't be blank");

这将显示错误消息,如图所示,带有默认错误图标。您甚至可以使用 setError 方法的第二个变体来更改图标。

public void setError (CharSequence error, Drawable icon)

要清除错误,只需将 null 作为方法参数传递。

textView.setError(null);

更多细节检查: http: //developer.android.com/reference/android/widget/TextView.html#setError(java.lang.CharSequence)

于 2012-10-27T20:58:42.263 回答
3

除了使用 Toast 之外,您还可以随时使用新的 Crouton 库:

http://www.grokkingandroid.com/useful-android-libraries-crouton/

于 2012-10-27T18:45:58.470 回答
0

-你可以用它Toast来做到这一点。

请参阅此链接:

http://www.mkyong.com/android/android-toast-example/

-您也可以使用它AlertDialog来执行此操作。

http://www.mkyong.com/android/android-alert-dialog-example/

于 2012-10-27T18:40:48.787 回答
0

使用Toast.makeText()如下:

 try{
    .....
    ....
  }catch(NullPointerException ex){ //<--Exception when you want to show the message
      Toast.makeText(getApplicationContext(), ex.getMessage(), 100).show(); 
  }

对于验证消息:

  if(textFieldValue == null || "".equals(textFieldValue)){
     Toast.makeText(getApplicationContext(), "Field can't be blank", 100).show(); 
  }
于 2012-10-27T18:42:53.377 回答