问题
I am asking this question after going through every possible way of resolving it.
I have two EditText defined as follows
private EditText firstText;
private EditText secondText;
I am resetting the value of one EditText on change of the other and vice versa. I was getting a stackOverFlow error because of the very same reason of calling EditText recursively.
Hence, I followed this link to solve my problem
Luckily, things work but only for secondText not for the firstText. This means when I change value for secondText, I see updated value in firstText. But same does not go for firstText.
When I tried debugging by printing a garbbish value I got to know that firstText is not calling on addTextChangedListener.
Here is the code for both of my EditText
firstTextTW = 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) {
System.out.println("I am inside firstTextTW");
int afterCount = firstText.getText().length();
System.out.println(afterCount);
if(afterCount == 1) {
//if the character is null then do nothing
}else{
System.out.println("Inside firstTextTw");//debugging by printing
Double number = Double.parseDouble(s.toString());
String SetCurrency = actv.getText().toString();
String SecondCurrency = actv2.getText().toString();
SetCurrency = SetCurrency.trim();
SecondCurrency = SecondCurrency.trim();
SecondTask task = new SecondTask();
try {
double CurrencyDiff;
CurrencyDiff = Double.parseDouble(task.execute("http://api.fixer.io/latest?base=" + SetCurrency + "&symbols=" + SetCurrency + "," + SecondCurrency).get());
Double final_result = number * CurrencyDiff;
Double final_value = Math.round(final_result * 1000.0) / 1000.0;
secondText.removeTextChangedListener(secondTextTW);
secondText.setText(final_value.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
secondTextTW = 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) {
Double number_to_be_divided_from = Double.parseDouble(s.toString());
String auto1 = actv.getText().toString();
String auto2 = actv2.getText().toString();
auto1 = auto1.trim();
auto2 = auto2.trim();
SecondTask task = new SecondTask();
Double currency_converter;
try {
currency_converter = Double.parseDouble(task.execute("http://api.fixer.io/latest?base=" + auto1 + "&symbols=" + auto1 + "," + auto2).get());
System.out.println(currency_converter);
Double resultToUpdate = number_to_be_divided_from / currency_converter;
Double resultToUpdateFinal = Math.round(resultToUpdate * 1000.0) / 1000.0;
//System.out.println(resultToUpdateFinal);
firstText.removeTextChangedListener(firstTextTW);
firstText.setText(resultToUpdateFinal.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
firstText.addTextChangedListener(firstTextTW) ;
secondText.addTextChangedListener(secondTextTW);
As one can see I am calling addTextChangedListener on both the EditText and I have even defined firstTextTW and secondTextTW properly as global variables just below the two EditText
Thanks in advance
Edit- code for setOntouchListener used for num keypad
firstText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "9"));
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "8"));
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "7"));
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "6"));
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "5"));
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "4"));
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "3"));
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "2"));
}
});
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "1"));
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int text_count = firstText.getText().length();
if(text_count == 1) {
}else{
firstText.setText(firstText.getText().insert(firstText.getText().length(), "0"));
}
}
});
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstText.setText(firstText.getText().insert(firstText.getText().length(), "."));
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = firstText.getText().length();
if(count==1)
{
firstText.setText(" ");
System.out.println("Replace text with blank space");
}
else {
firstText.setText(firstText.getText().delete(firstText.getText().length() - 1, firstText.getText().length()));
}
}
});
}
return true;
}
});
回答1:
You do as like
mSearchbox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
ivCancelSearch.setVisibility(mSearchbox.getText().length() > 0 ? View.VISIBLE : View.GONE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
similer for other remember get correct id of edittext
来源:https://stackoverflow.com/questions/35917419/addtextchangedlistener-works-for-one-edittext-but-not-for-the-other