简体   繁体   中英

Android: EditText input validation

I am building an app where I have an EditText into which a byte value needs to be inserted. The EditText should only be able to accept the values -128 to 127 which are the Byte.MIN_VALUE and Byte.MAX_VALUE .

I created a class called EditTextFilter which implements TextWatcher . I use the afterTextChanged event listener to check the value. I am handling this in a separate class as I use this for several EditText objects. Please see the code below:

@Override
public void afterTextChanged(Editable s)
{
    double value = 0;

    try
    {
        value = Double.parseDouble(s.toString());
    }
    catch (NumberFormatException e)
    {
        Log.e(EditTextFilter.class.toString(), e.getMessage());
    }

    switch (this.type)
    {
        case TYPE_BYTE:
            Log.v(EditTextFilter.class.toString(), "-----> EditTextFilter Byte");
            if (value > Byte.MAX_VALUE)
            {
                Log.v(EditTextFilter.class.toString(), "-----> Value out of range. Greater than Byte.MAX_VALUE");
                Toast.makeText(context, "Value out of range. Greater than Byte.MAX_VALUE", Toast.LENGTH_SHORT).show();
            }
            else if (value <= Byte.MIN_VALUE)
            {
                Log.v(EditTextFilter.class.toString(), "-----> Value out of range. Less than Byte.MIN_VALUE");
                Toast.makeText(context, "Value out of range. Less than Byte.MIN_VALUE", Toast.LENGTH_SHORT).show();
            }
        break;
        case TYPE_CHAR:
            Log.v(EditTextFilter.class.toString(), "-----> EditTextFilter Char");
            if (s.toString().length() > 1)
            {
                Log.v(EditTextFilter.class.toString(), "-----> Please enter single character");
                Toast.makeText(context, "Please enter a single character", Toast.LENGTH_SHORT).show();
            }
    }
}

I then call the class from my byte handler. as follows:

    // Handle the text change to validate input
    EditTextFilter.addTextChangedListener(editText, EditTextFilter.TYPE_BYTE);

As you can see I am only checking the value, firing a Toast to tell the user but this does not change the value which can still cause damage in my Database . How can I ensure that the value entered is within range and if it is less than Byte.MIN_VALUE or more than Byte.MAX_VALUE then set it to the min or max value. I know this may come across a little confusing but I hope you guys understand what I am asking.

I appreciate any help in this matter. I have looked at Is there a way to define a min and max value for EditText in Android? but it does not specify how to handle numbers larger than int and also does not explain how to change the EditText from the EditTextFilter class.

Thank you in advance

You will want to either reset the value of the EditText or set it to a value you specify. Below is an example I've used in the past for MAX/MIN INT values. It should be similar to what you want to do with the BYTE values. Just get the contents of EditText, remove the last character via substring, then write it back to the EditText.

@Override
public void afterTextChanged(Editable s) {

    if (s.length() == 0) {

    } else {
        Integer cap = Integer.valueOf(etCapture.getText().toString());
        if (cap > MAX) {
            Toast.makeText(SettingsActivity.this, "Out of range. Maximum capture number is " + MAX, Toast.LENGTH_SHORT).show();
            String strCap = etCapture.getText().toString();
            strCap = strCap.substring(0,strCap.length()-1);
            etCapture.setText(strCap);
            etCapture.setSelection(strCap.length());
        } else if (cap < MIN) {
            Toast.makeText(SettingsActivity.this, "Out of range. Minimum capture number is " + MIN, Toast.LENGTH_SHORT).show();
            etCapture.setText(MIN.toString());
        } else {
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putInt("cap", cap);
            editor.apply();
        }
    }
}

Thanks for the reply. I fixed the issue by sending the ID of the EditText to the EditTextFilter and then using the id and EditText et = (EditText)findViewById(id);

Please see below snippet if you need help:

        case TYPE_SHORT:
            Log.v(EditTextFilter.class.toString(), "-----> EditTextFilter Short");

            editText = (EditText)thisTask.findViewById(thisId);
            if (value > Short.MAX_VALUE)
            {
                editText.setText(String.valueOf(Short.MAX_VALUE));
                Log.v(EditTextFilter.class.toString(), "-----> Value out of range. Greater than Byte.MAX_VALUE. Setting to 127");
                Toast.makeText(context, "Value out of range, setting to MAX", Toast.LENGTH_SHORT).show();
                // Remove the text change listener. If not removed additional text change listener
                // will be created by the caller using addTextChangeListener method
                editText.removeTextChangedListener(this);
            }
            else if (value <= Short.MIN_VALUE)
            {
                editText.setText(String.valueOf(Short.MIN_VALUE + 1));
                Log.v(EditTextFilter.class.toString(), "-----> Value out of range. Less than Short.MIN_VALUE");
                Toast.makeText(context, "Value out of range, setting to MIN", Toast.LENGTH_SHORT).show();
                // Remove the text change listener. If not removed additional text change listener
                // will be created by the caller using addTextChangeListener method
                editText.removeTextChangedListener(this);
            }

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