eText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
int wordvalue = 0;
int numvalue = 0;
for(int i=0; i<s.length(); i++) {
wordvalue += this.calcCharValue(s.charAt(i));
numvalue = this.calcCharValue(s.charAt(i));
String userEntered = nText.getText().toString();
userEntered = userEntered + numvalue;
tv.setText(String.valueOf(wordvalue));
nText.setText(String.valueOf(userEntered));
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
protected int calcCharValue(char a) {
Character ch = new Character(a);
if(ch >= 'A' && ch <= 'Z') { // a=1, b=2, c=3, ..., z=26
Character ca = new Character('A');
return ch.charValue() - ca.charValue() + 1;
}
我尝试创建一个从单词中获取文本值的应用程序。
例如,a = 1,b = 2,
我能够像abc = 6,abcd = 10
一样添加它们现在我的问题是我将此值视为
abc = 123和abcde = 12345
但我得到的是
abc = 112123 and abcde = 1 12 123 1234 12345
重复之前的每个字符,然后添加下一个值,以便如何处理这个重复的问题。