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

发布于 2024-10-20 19:04:19 字数 868 浏览 3 评论 0 原文

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

据我所知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。

I want to show all my validation error's of EdiText fields in a popup as shown in below image:

Error alert in popup

As far as I know Android has 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

I am able to display the red error indicator inside the right side of the EditText by using:

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

Now also i want to display the error message as shown is the first image but it seems I am not getting any idea about this, though I think it should be a Custom Toast.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離人涙 2024-10-27 19:04:19

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

默认可绘制

默认可绘制

自定义可绘制

Custom Drawable

因此,我刚刚在布局 xml 文件中创建了两个 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("^[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 字段保持为空,则错误将显示如下图所示:

Empty文本><br />
<img src=

对于错误的输入验证:

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

错误的用户名

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

错误密码

注意:

这里我仅在用户将 EditText 字段留空并按键盘上的 Next 键的情况下使用自定义绘图,但您可以在任何情况下使用它。只需要在 setError() 方法中提供 Drawable 对象即可。

As the earlier answer is solution for my problem but I have tried a different approach to use a custom Drawable image instead of default indicator_input_error image.

Default Drawable

Default Drawable

Custom Drawable

Custom Drawable

So, I have just created two EditText in my layout xml file and then implemented some Listener in Java code on that 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;
        }
    }
}

Now I have tested it like:

For empty EditText validations :

Suppose user click on the Username field then Softkeybord opens and if user press Next key then the user will be focused to the Password field and Username field remains empty then the error will be shown like as given in below images:

Empty text
Empty text

For wrong input validations :

1) I type the text vikaS in Username field then error will be like as given in below image :

Wrong username

2) I type the text Password1 in password field then error will be like as given in below image :

wrong password

Note:

Here I have used custom drawable only in case of when user left the EditText field blank and press Next key on key board but you can use it in any case. Only you need to supply Drawable object in setError() method.

絕版丫頭 2024-10-27 19:04:19

请尝试以下操作:

final EditText editText = (EditText) findViewById(R.id.edit);

editText.setImeActionLabel("", EditorInfo.IME_ACTION_NEXT);

editText.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            if (editText.getText().toString().isBlank())
                editText.setError("Please enter something.");
            else
                Toast.makeText(getApplicationContext(), "Entered: " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
        }
        return false;
    }
});

Try the following:

final EditText editText = (EditText) findViewById(R.id.edit);

editText.setImeActionLabel("", EditorInfo.IME_ACTION_NEXT);

editText.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            if (editText.getText().toString().isBlank())
                editText.setError("Please enter something.");
            else
                Toast.makeText(getApplicationContext(), "Entered: " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
        }
        return false;
    }
});
没有你我更好 2024-10-27 19:04:19

我知道答案已被提问者接受,但以上内容都不适合我。

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

这是我让它发挥作用的方法。

  1. 创建主题:

    
    

  2. MyApp.Theme.Light.NoTitleBar 主题从清单应用到我的应用程序/活动。

    <前><代码> <应用程序
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyApp.Theme.Light.NoTitleBar"
    >

I know answer has been accepted by the asker, but none of the above worked for me.

I was able to reproduce this on my Nexus S running Android 4.0.3.

Here's how I made it work.

  1. Create a theme with:

    <style name="MyApp.Theme.Light.NoTitleBar" parent="@android:style/Theme.Light.NoTitleBar">
         <item name="android:textColorPrimaryInverse">@android:color/primary_text_light
         </item>
    </style>
    
  2. Apply MyApp.Theme.Light.NoTitleBar theme to my application / activity from manifest.

        <application
             android:name=".MyApp"
             android:icon="@drawable/ic_launcher"
             android:label="@string/app_name" 
             android:theme="@style/MyApp.Theme.Light.NoTitleBar"
        >
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文