L
L
lahomie932018-09-10 17:22:03
Android
lahomie93, 2018-09-10 17:22:03

How to perform validation in EditText in Android?

Hello! My task is to develop an EditText view into which we enter a phone. In Edit Text, we see a hint like this: +7(___)___-__-__ The user starts typing numbers right after 7, and as characters are typed, underscores remain. I don't know where to start solving this problem, and I need a hint in which direction I need to go. Perhaps you have a ready-made solution and please share it)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrei, 2018-09-10
@umpteenthdev

There is a good solution: https://github.com/RedMadRobot/input-mask-android . But if you really want to do it yourself - look towards InputFilters

D
Denis, 2018-09-11
@akaish

This is done using the TextWatcher listener:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

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

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});

Each time you change the input field, you use one of these methods to format the string according to your template.
UPD. If required, I will find a snippet for formatting a phone number, I already wrote a similar one. In short, the formatting itself can be done using the Google libphonenumber library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question