Add input constraints to Edit Text in Android?

2019-05-28 22:33发布

问题:

I have condition where it is required to enter some numeric values in the application.

Cases:

  1. If user enters whole number then the max length should be 8 digits. Eg: 12345678

  2. If users wants to enter decimal value he can only input upto 2 decimal points. Eg: 1.03, 123.95

  3. If users enters whole 8 digits then he can also input the decimal places upto 2 decimal points. Eg: 12345678.12

So we have the following valid inputs:

     1.  12345678
     2.  123.45
     3.  12345678.98

How can I achieve the above case scenario?

回答1:

Here you go. This is exactly what you want...

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.InputFilter;
    import android.text.Spanned;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.widget.EditText;

    public class EditTextLogic extends Activity
    {
        EditText mEditText;
        String TEMP="",INPUT="";
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.edit_tesxt_logic);  
            mEditText =(EditText) findViewById(R.id.textView1);

            mEditText.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}

                @Override
                public void afterTextChanged(Editable Token) 
                {
                    if(Token.toString().length()<INPUT.length())
                    {
                        INPUT=Token.toString();
                    }
                }
            });
            mEditText.setFilters(new InputFilter[] {
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence input, int arg1,
                                int arg2, Spanned arg3, int arg4, int arg5) 
                        {
                            CharSequence returned = validate(input.toString());
                            return returned;
                        }
                    }
            });

        }

        private CharSequence validate(String Token) {
            Log.i("Token",""+Token);
            TEMP = INPUT;
            TEMP += Token; 
            if(TEMP.contains("."))
            {
                try
                {
                    String FractionNo = TEMP.split("\\.")[1];
                    if(FractionNo.length()> 2)
                    {

                        return "";
                    }
                    else
                    {
                        INPUT = TEMP;
                        return Token;
                    }
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    INPUT = TEMP;
                    return Token;
                }
            }
            else
            {
                if(TEMP.length()>8)
                {
                    return "";
                }
                else
                {
                    INPUT = TEMP;
                    return Token;
                }
            }
        }
    }

And this is your XML:

<EditText
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:singleLine="true"/>

Cheers! Enjoy Coding!!