Is there a way to define a min and max value for EditText in Android?

后端 未结 25 1629
你的背包
你的背包 2020-11-22 11:51

I want to define a min and max value for an EditText.

For example: if any person tries to enter a month value in it, the value must be between 1-12.

25条回答
  •  失恋的感觉
    2020-11-22 12:42

    please check this code

        String pass = EditText.getText().toString(); 
        if(TextUtils.isEmpty(pass) || pass.length < [YOUR MIN LENGTH]) 
        { 
           EditText.setError("You must have x characters in your txt"); 
            return; 
        }
    
        //continue processing
    
    
    
    edittext.setOnFocusChangeListener( new OnFocusChangeListener() {
    
           @Override
           public void onFocusChange(View v, boolean hasFocus) {
              if(hasFocus) {
               // USE your code here 
      }
    

    USe the below link for more details about edittext and the edittextfilteres with text watcher..

    http://www.mobisoftinfotech.com/blog/android/android-edittext-setfilters-example-numeric-text-field-patterns-and-length-restriction/

提交回复
热议问题