From d7b423e95370b5b436efbbc0882fb79433de0fc1 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 15 Oct 2011 15:28:09 +0400 Subject: [PATCH] characters filter for var name --- res/layout/var_edit.xml | 1 - res/values-ru/strings.xml | 1 + res/values/strings.xml | 1 + .../calculator/CalculatorVarsActivity.java | 28 +++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/res/layout/var_edit.xml b/res/layout/var_edit.xml index ef7018ff..6d5edfad 100644 --- a/res/layout/var_edit.xml +++ b/res/layout/var_edit.xml @@ -26,7 +26,6 @@ diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index 9907aab4..642e793d 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -74,5 +74,6 @@ M История пуста! История + Символ \'%s\' не допустим в названии переменной! diff --git a/res/values/strings.xml b/res/values/strings.xml index fb303679..a0efd8a9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -74,4 +74,5 @@ M History is empty! History + Character \'%s\' is not accepted in variable name! diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 53185070..4b508a35 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -12,6 +12,8 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.view.*; import android.widget.*; import org.jetbrains.annotations.NotNull; @@ -23,6 +25,7 @@ import org.solovyev.android.calculator.model.VarsRegister; import org.solovyev.common.utils.StringUtils; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -32,6 +35,8 @@ import java.util.List; */ public class CalculatorVarsActivity extends ListActivity { + private final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray())); + @NotNull private VarsArrayAdapter adapter; @@ -82,8 +87,31 @@ public class CalculatorVarsActivity extends ListActivity { final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); final View editView = layoutInflater.inflate(R.layout.var_edit, null); + final String errorMsg = CalculatorVarsActivity.this.getString(R.string.c_char_is_not_accepted); + final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); editName.setText(name); + editName.addTextChangedListener(new TextWatcher() { + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + for ( int i = 0; i < s.length(); i++ ) { + char c = s.charAt(i); + if (!acceptableChars.contains(c)) { + s.delete(i, i+1); + Toast.makeText(CalculatorVarsActivity.this, String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); + } + } + } + }); final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); editValue.setText(value);