如何在弹出窗口中显示输入错误?

时间:2011-03-07 10:46:26

标签: android validation popup android-edittext

我想在弹出窗口中显示EdiText字段的所有验证错误,如下图所示:

Error alert in popup

据我所知,Android有drawables:

  

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

现在我也想显示错误信息,如图所示是第一张图片,但似乎我对此没有任何了解,虽然我认为它应该是自定义Toast。

3 个答案:

答案 0 :(得分:38)

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

默认Drawable

Default Drawable

Custom Drawable

Custom Drawable

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

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("^[a-z]{1,16}$", s)) {
                        et.setError("Oops! Username must have only a-z");
                    }
                }
                    break;

                case R.id.etPassword: {
                    if (!Pattern.matches("^[a-zA-Z]{1,16}$", s)) {
                        et.setError("Oops! Password must have only a-z and A-Z");
                    }
                }
                    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;
        }
    }
}

现在我测试了它:

  

对于空的EditText验证:

假设用户点击Username字段,然后Softkeybord就会打开,如果用户按下Next键,则用户将专注于Password字段,Username字段仍为空然后错误将显示如下图所示:

Empty text Empty text

  

错误的输入验证:

1)我在用户名字段中键入文本vikaS,然后错误将如下图所示:

Wrong username

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

wrong password

注意:

这里我只使用了自定义drawable,只有当用户将EditText字段留空并按下键盘上的Next键时,你可以在任何情况下使用它。只需要在Drawable方法中提供setError()对象。

答案 1 :(得分:34)

试试这个..

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

答案 2 :(得分:1)

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

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

以下是我如何使其发挥作用。

  1. 使用以下内容创建主题:

    <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"
        >