[英]Android: EditText input validation
我正在構建一個應用程序,其中有一個需要插入字節值的EditText
。 EditText
應該只能接受值 -128 到 127,即Byte.MIN_VALUE
和Byte.MAX_VALUE
。
我創建了一個名為EditTextFilter
的class
,它實現了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
,也沒有解釋如何將更改EditText
從EditTextFilter
類。
先感謝您
您需要重置 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.