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

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

    this is my code max=100, min=0

    xml

    
    

    java

    EditText ed = findViewById(R.id.txt_Mass_smallWork);
        ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {`
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!charSequence.equals("")) {
                    int massValue = Integer.parseInt(charSequence.toString());
                    if (massValue > 10) {
                        ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
                    } else {
                        ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
                    }
                }
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
            }
        });
    

提交回复
热议问题