I Am Unable To Change Text In Edittext1 In Response To The Text Change In Edittext2
I want the user to enter a temperature in Celsius and display the temperature in fahrenheit and vice versa. I think TextChangedListener would be perfect to convert without any butt
Solution 1:
I would implement two TextViews
to display the result of each calculation. What you are doing now is creating an infinite loop, when changing text in the first EditText
trigger your second edit texts onTextChanged
listener, and so on. Hence the stackoverflow error.
Change your implementation to display each EditText
calculation in a TextView
, then the problem will be gone.
In short, add your TextView
TextViewtv= (TextView)findViewById(R.id.textview);
and in your
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
c = Float.parseFloat(s.toString());
f = (((9 / 5) * c) + 32);
tv .setText(String.valueOf(f));
}
Solution 2:
try that code
i have just studied the documentation and it says that
onTextChanged() is not a place to modify text of EditText
here is the link for documentation you can verify from there
so wahat you can do is to move the code to after text change.
C.addTextChangedListener(newTextWatcher() {
float f
,
c;
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
}
@OverridepublicvoidafterTextChanged(Editable s) {
if (!isAlpha(s.toString)){
if(!s.toString().equals("")){
c = Float.parseFloat(s.toString());
f = (((9 / 5) * c) + 32);
F.setText(""+ f);
}
}
}
});
this isAlpha function is to check whether user entered numeric value or alphabatic value
publicbooleanisAlpha(String name) {
return name.matches("[a-zA-Z]+");
}
Post a Comment for "I Am Unable To Change Text In Edittext1 In Response To The Text Change In Edittext2"