开发者

How to display input errors in popup?

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

How to display input errors in popup?

As far as I know Android has drawables:

1) popup_inline_error.9.png

How to display input errors in popup?

2) popup_inline_erro开发者_如何学Pythonr_above.9.png

How to display input errors in popup?

3) indicator_input_error.png

How to display input errors in popup?

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.


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

How to display input errors in popup?

Custom Drawable

How to display input errors in popup?

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:

How to display input errors in popup?

How to display input errors in popup?

For wrong input validations :

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

How to display input errors in popup?

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

How to display input errors in popup?

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.


try this..

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜