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

后端 未结 25 1220
你的背包
你的背包 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:30

    I made a simpler way to set a min/max to an Edittext. I use arithmetic keypad and I work with this method:

     private int limit(EditText x,int z,int limin,int limax){
    
        if( x.getText().toString()==null || x.getText().toString().length()==0){
            x.setText(Integer.toString(limin));
            return z=0;
        }
        else{
            z = Integer.parseInt(x.getText().toString());
             if(z limax){
                 if(z<10){
                     x.setText(Integer.toString(limin));
                    return  z=0;
                 }
                else{
                    x.setText(Integer.toString(limax));
                    return z=limax;
                }
    
             }
             else
                return z = Integer.parseInt(x.getText().toString());
        }
     }
    

    The method accepts all of your values but if a value of users in not adhere to your limits it will be set automatically to the min/max limit. For ex. limit limin=10, limax =80 if the user sets 8, automatically 10 is saved to a variable and EditText is set to 10.

提交回复
热议问题