How to set Min text (Mandatory) and Max text in EditText

烈酒焚心 提交于 2019-12-03 11:04:06

or you can just change your code TO this

value.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            if (hasFocus) {
           if (value.getText().toString().trim()     **try using value.getText().length()<3**
                        .length() < 3) {             **instead of the value.getText().trim().length()**

               value.setError("Failed");
                    } else {
                        value.setError(null);
                    }}}});

You can try this code

First of all you set your maxlength in xml file like this

                    <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:inputType="textPassword"
                    android:lines="1"
                    android:maxLength="15"
                    android:maxLines="1"
                    android:singleLine="true" />

Then in your code you can write like this

et_billamt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (et_billamt.getText().toString().trim().length() < 5) {
                    et_billamt.setError("Failed");
                } else {
                    // your code here
                    et_billamt.setError(null);
                }
            } else {
                if (et_billamt.getText().toString().trim().length() < 5) {
                    et_billamt.setError("Failed");
                } else {
                    // your code here
                    et_billamt.setError(null);
                }
            }

        }
    });

I designed if after no focus, so here you can write for min length condition and max length condition

Try this
EditText value =(EditText) findviewbyId(R.id.urEditTextId);// this line must be oncreate

// place these line where u want to check

String ed1=value .getText().toString();
int size=ed1.length();

you can match the digit and perform appropriate action

if(size==0)
//Toast : kindly enter atleast one letter 

if(size>175)
//Toast : max length 175 char 

you can extend the EditText class, and override the onTextChanged method to monitor the text length change by yourself. Then you can control the limitation.

public class CustomEditText extends EditText{

public CustomEditText(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
protected void onTextChanged(CharSequence text, int start,
        int lengthBefore, int lengthAfter) {
    // TODO Auto-generated method stub
    Log.i("length", "input text length = " + text.length());
    super.onTextChanged(text, start, lengthBefore, lengthAfter);
}

}

Here's an EditText and a Button:

EditText myEditText;
Button button;

In your onCreate():

myEditText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.button);

Say you submit or use the value that a user types in the myEditText using the button. Set an 'OnClickListener' to this button:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if (mEditText.length() < 1 ||
            mEditText.length() > 175) {

               mEditText.setError("Value should be between 1 and 175 characters in length");

               mEditText.requestFocus();
        } else {

            // Value is valid (between 1 and 175 characters long)

               String text = mEditText.getText().toString();

            // submit
        } 
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!