characters filter for var name

This commit is contained in:
serso 2011-10-15 15:28:09 +04:00
parent 56f224cd16
commit 91acb760f1
4 changed files with 30 additions and 1 deletions

View File

@ -26,7 +26,6 @@
<EditText a:id="@+id/var_edit_name" <EditText a:id="@+id/var_edit_name"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:digits="abcdefghijklmnopqrstuvwxyz1234567890"
a:textSize="20dp"> a:textSize="20dp">
</EditText> </EditText>

View File

@ -74,5 +74,6 @@
<string name="c_history_button">M</string> <string name="c_history_button">M</string>
<string name="c_history_is_empty">История пуста!</string> <string name="c_history_is_empty">История пуста!</string>
<string name="c_app_history">История</string> <string name="c_app_history">История</string>
<string name="c_char_is_not_accepted">Символ \'%s\' не допустим в названии переменной!</string>
</resources> </resources>

View File

@ -74,4 +74,5 @@
<string name="c_history_button">M</string> <string name="c_history_button">M</string>
<string name="c_history_is_empty">History is empty!</string> <string name="c_history_is_empty">History is empty!</string>
<string name="c_app_history">History</string> <string name="c_app_history">History</string>
<string name="c_char_is_not_accepted">Character \'%s\' is not accepted in variable name!</string>
</resources> </resources>

View File

@ -12,6 +12,8 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*; import android.view.*;
import android.widget.*; import android.widget.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -23,6 +25,7 @@ import org.solovyev.android.calculator.model.VarsRegister;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@ -32,6 +35,8 @@ import java.util.List;
*/ */
public class CalculatorVarsActivity extends ListActivity { public class CalculatorVarsActivity extends ListActivity {
private final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray()));
@NotNull @NotNull
private VarsArrayAdapter adapter; private VarsArrayAdapter adapter;
@ -82,8 +87,31 @@ public class CalculatorVarsActivity extends ListActivity {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null); 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); final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(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); final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
editValue.setText(value); editValue.setText(value);