Limit an EditText from 1 to 100 and include the symbol "%"

1

I am making an application that takes the data entered in a EditText and does a certain calculation.

The problem is that I need the field to go from 1 to 100%, but the mask I'm using is too simple for this, so it only makes the limitation but goes from 0 to 999 and it's not that that I want.

I also can not put the% symbol at the end of this field.

Follow the code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import com.github.rtoshiro.util.format.MaskFormatter;
import com.github.rtoshiro.util.format.SimpleMaskFormatter;
import com.github.rtoshiro.util.format.pattern.MaskPattern;
import com.github.rtoshiro.util.format.text.MaskTextWatcher;

public class ActivityForm extends AppCompatActivity {


private EditText percentual;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_form);

    percentual = (EditText) findViewById(R.id.percentual_Id);
    SimpleMaskFormatter simpleMaskPercentual = new SimpleMaskFormatter( " NNN% " );
    MaskTextWatcher maskPercentual = new MaskTextWatcher(percentual, simpleMaskPercentual);
    percentual.addTextChangedListener( maskPercentual );
    }
}
    
asked by anonymous 27.07.2018 / 00:26

1 answer

1

By analyzing the MaskFormatter code in GitHub , I think the easiest approach is to implement TextWatcher directly. MaskFormatter does not appear to have been made for a case like what you have.

I'm going to use an approach based on regular expression. The idea is that it validates the input according to a regular expression. If the entry is not valid, discard the last character and try again until the resulting string is valid or until it becomes empty. This is what the format(String) method does.

The replacement I think would be this:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import java.util.regex.Pattern;

public class ActivityForm extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_form);
        EditText percentual = (EditText) findViewById(R.id.percentual_Id);
        PercentTextWatcher mask = new PercentTextWatcher(percentual);
        percentual.addTextChangedListener(mask);
    }

    private static class PercentTextWatcher implements TextWatcher {

        private static final Pattern PERC_PAT =
                Pattern.compile("(?:[1-9]|[1-9][0-9]|100)%?");

        private final TextView textView;

        private String currentText;

        public PercentTextWatcher(TextView textView) {
            this.regex = regex;
        }

        @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.toString().equals(currentText)) {
                currentText = formatter.format(charSequence.toString());
                textView.setText(currentText);
                if (textView instanceof EditText) {
                    EditText editText = (EditText) textView;
                    editText.setSelection(currentText.length());
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }

        private static String format(String in) {
            if (in == null) return "";
            for (String s = in; !s.isEmpty(); s = s.substring(0, s.length() - 1)) {
                if (PERC_PAT.matcher(s).matches()) return s;
            }
            return "";
        }
    }
}

Here's a test of the format(String) method of the above code.

Note that there are two classes in this file. The PercentTextWatcher class is used to replace MaskTextWatcher .

If you prefer to use 0% to 100% instead of 1% to 100%, just change the regular expression to "(?:[0-9]|[1-9][0-9]|100)%?" instead of "(?:[1-9]|[1-9][0-9]|100)%?" .

    
27.07.2018 / 01:48