如何在popup窗口中显示input错误?

我想在popup窗口中显示我所有的EdiText字段的validation错误,如下图所示:

弹出错误提示

据我所知Android可以绘制:

1)popup_inline_error.9.png

popup_inline_error.9.png

2)popup_inline_error_above.9.png

popup_inline_error_above.9

3)indicator_input_error.png

indicator_input_error.png

我可以通过使用以下内容来显示EditText右侧的红色错误指示器:

 Drawable err_indiactor = getResources().getDrawable(R.drawable.indicator_input_error); mEdiText.setCompoundDrawablesWithIntrinsicBounds(null, null, err_indiactor, null); 

现在我也想显示错误消息,如图所示是第一个图像,但似乎我没有得到任何关于这个,但我认为它应该是一个自定义面包。

尝试这个..

 final EditText editText=(EditText) findViewById(R.id.edit); editText.setImeActionLabel("",EditorInfo.IME_ACTION_NEXT); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if(actionId==EditorInfo.IME_ACTION_NEXT){ if( editText.getText().toString().trim().equalsIgnoreCase("")) editText.setError("Please enter some thing!!!"); else Toast.makeText(getApplicationContext(),"Notnull",Toast.LENGTH_SHORT).show(); } return false; } }); 

由于早期的答案是我的问题的解决scheme,但我已经尝试了一种不同的方法来使用自定义可绘制图像,而不是默认的indicator_input_error图像。

默认可绘制

默认可绘制

自定义可绘制

自定义可绘制

所以,我刚刚在我的layout xml文件中创build了两个EditText ,然后在EditText上用Java代码实现了一些Listener

main.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dip" android:background="#222222"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:id="@+id/etUsername" android:singleLine="true" android:imeActionLabel="Next"></EditText> <EditText android:layout_width="match_parent" android:inputType="textPassword" android:layout_height="wrap_content" android:hint="Password" android:id="@+id/etPassword" android:singleLine="true" android:imeActionLabel="Next"></EditText> </LinearLayout> 

EditTextValidator.java

 import java.util.regex.Pattern; import android.app.Activity; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; public class EditTextValidator extends Activity { private EditText mUsername, mPassword; private Drawable error_indicator; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Setting custom drawable instead of red error indicator, error_indicator = getResources().getDrawable(R.drawable.emo_im_yelling); int left = 0; int top = 0; int right = error_indicator.getIntrinsicHeight(); int bottom = error_indicator.getIntrinsicWidth(); error_indicator.setBounds(new Rect(left, top, right, bottom)); mUsername = (EditText) findViewById(R.id.etUsername); mPassword = (EditText) findViewById(R.id.etPassword); // Called when user type in EditText mUsername.addTextChangedListener(new InputValidator(mUsername)); mPassword.addTextChangedListener(new InputValidator(mPassword)); // Called when an action is performed on the EditText mUsername.setOnEditorActionListener(new EmptyTextListener(mUsername)); mPassword.setOnEditorActionListener(new EmptyTextListener(mPassword)); } private class InputValidator implements TextWatcher { private EditText et; private InputValidator(EditText editText) { this.et = editText; } @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() != 0) { switch (et.getId()) { case R.id.etUsername: { if (!Pattern.matches("^[az]{1,16}$", s)) { et.setError("Oops! Username must have only az"); } } break; case R.id.etPassword: { if (!Pattern.matches("^[a-zA-Z]{1,16}$", s)) { et.setError("Oops! Password must have only az and AZ"); } } break; } } } } private class EmptyTextListener implements OnEditorActionListener { private EditText et; public EmptyTextListener(EditText editText) { this.et = editText; } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_NEXT) { // Called when user press Next button on the soft keyboard if (et.getText().toString().equals("")) et.setError("Oops! empty.", error_indicator); } return false; } } } 

现在我已经testing它,如:

对于空的EditTextvalidation:

假设用户点击Username段,然后Softkeybord打开,如果用户按下Next键,则用户将被关注到Password字段,并且Username段保持空白,然后错误将显示如下图所示:

空的文本空的文本

对于错误的inputvalidation:

1)我input用户名字段中的文本vikaS然后错误将如下图所示:

错误的用户名

2)我在密码字段中键入文本Password1 ,那么错误将如下图所示:

密码错误

注意:

在这里,我只使用了自定义绘图,当用户离开EditText字段时,按键盘上的下一个键,但你可以在任何情况下使用它。 只有你需要在setError()方法中提供Drawable对象。

我知道答案已经被提问者接受了,但是以上都没有为我工作。

我能够在运行Android 4.0.3的Nexus S上重现这一点。

以下是我如何使其工作。

  1. 创build一个主题:

     <style name="MyApp.Theme.Light.NoTitleBar" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:textColorPrimaryInverse">@android:color/primary_text_light </item> </style> 
  2. MyApp.Theme.Light.NoTitleBar主题应用于我的应用程序/活动。

      <application android:name=".MyApp" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/MyApp.Theme.Light.NoTitleBar" >