Android:EditText输入验证

时间:2015-11-09 13:18:14

标签: android validation input android-edittext

我正在构建一个应用程序,其中我有一个EditText,需要插入一个字节值。 EditText应该只能接受Byte.MIN_VALUEByte.MAX_VALUE的值-128到127。

我创建了一个名为class的{​​{1}},它实现了EditTextFilter。我使用TextWatcher事件侦听器来检查值。我在单独的afterTextChanged中处理此问题,因为我将其用于多个class个对象。请参阅以下代码:

EditText

然后我从我的@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(); } } } 处理程序中调用该类。如下:

byte

正如您所看到的,我只检查了该值,触发 // Handle the text change to validate input EditTextFilter.addTextChangedListener(editText, EditTextFilter.TYPE_BYTE); 告诉用户,但这并未改变仍可能导致Toast损坏的值。如何确保输入的值在范围内,如果小于Database或大于Byte.MIN_VALUE,则将其设置为最小值或最大值。我知道这可能会让人感到困惑,但我希望你们能理解我的要求。

我很感激这方面的任何帮助。我查看了Is there a way to define a min and max value for EditText in Android?,但没有说明如何处理大于Byte.MAX_VALUE的数字,也没有说明如何从int类更改EditText

提前谢谢

2 个答案:

答案 0 :(得分:1)

您需要重置EditText的值或将其设置为您指定的值。以下是我过去用于MAX / MIN INT值的示例。它应该类似于您想要使用BYTE值的操作。只需获取EditText的内容,通过substring删除最后一个字符,然后将其写回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();
        }
    }
}

答案 1 :(得分:0)

感谢您的回复。我通过将EditText的ID发送到EditTextFilter,然后使用ID和EditText et = (EditText)findViewById(id);

来解决了问题

如果您需要帮助,请参阅以下代码段:

        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);
            }