简体   繁体   中英

Range Validation for EditText in android

Can anyone tell me how to validate range of EditText in Android?? I want to limit my value not length.
For example,user will be able to give their weight between 20 to 400Kg only. So,is this possible with xml file? Or any other option for range validation of EditText ?

    EditText etExample= (EditText) getView().findViewById(R.id.etExample);
    etExample.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String text = s.toString().replaceAll("[^0-9]", "").Trim();
            System.out.println(Integer.valueOf(text) > 400 ? "Whale" : "Not a Whale");
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM