Reputation: 114
I want to complete a project in which the user writes different characters like A, D, C in an edit text box and then automatically the values of those characters are collected (Addition in each other) in another text box. For example if user pressed A then its value to textbox 10 and then user pressed B, its value 20 to textbox with A+B value. I am using the given code but not getting success.
your text
saif1.addTextChangedListener(new TextWatcher()
{
@SuppressLint("SetTextI18n")
@Override
public void afterTextChanged(Editable mEdit)
{
String text = mEdit.toString();
if (text.equals("A")) {
charactervalue.setText("10");
String n1 = charactervalue.getText().toString();
int n11 = Integer.parseInt(n1);
String n2 = result.getText().toString();
int n22 = Integer.parseInt(n2);
finaltotal.setText(String.valueOf(n11 + n22));
}else if(text.equals("B")) {
charactervalue.setText("20");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
}
});
Upvotes: 0
Views: 40
Reputation: 3062
Try this Code
binding.saif1.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 mEdit) {
String text = mEdit.toString();
int temp = 0;
for (char ch : text.toCharArray()) {
if (ch == 'A') {
temp += 10;
} else if (ch == 'B') {
temp += 20;
}
}
binding.textView.setText(String.valueOf(temp));
}
});
You can add more condition by duplicating else if
case.. e.g.
else if (ch == 'C') {
temp += 30;
}
Upvotes: 0