簡體   English   中英

Android:EditText 輸入驗證

[英]Android: EditText input validation

我正在構建一個應用程序,其中有一個需要插入字節值的EditText EditText應該只能接受值 -128 到 127,即Byte.MIN_VALUEByte.MAX_VALUE

我創建了一個名為EditTextFilterclass ,它實現了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或大於Byte.MAX_VALUE則將其設置為最小值或最大值。 我知道這可能會讓人有點困惑,但我希望你們明白我在問什么。

我很感激在這件事上的任何幫助。 我看過有沒有辦法在 Android 中定義 EditText 的最小值和最大值? 但它並沒有規定如何處理大於號int ,也沒有解釋如何將更改EditTextEditTextFilter類。

先感謝您

您需要重置 EditText 的值或將其設置為您指定的值。 下面是我過去用於 MAX/MIN INT 值的示例。 它應該類似於您想要對 BYTE 值執行的操作。 只需獲取 EditText 的內容,通過子字符串刪除最后一個字符,然后將其寫回 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();
        }
    }
}

謝謝回復。 我通過將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);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM