在经过各种可能的解决方法后,我问这个问题。
我有两个EditText定义如下
private EditText firstText;
private EditText secondText;
我正在重置另一个EditText的值,反之亦然。 我得到一个stackOverFlow错误,因为递归调用EditText的原因相同。
因此,我跟着this link to solve my problem
幸运的是,事情可行,但仅适用于第二个文本而不适用于第一个文本。这意味着当我更改secondText的值时,我在firstText中看到更新的值。但同样不适用于firstText。
当我通过打印garbbish值尝试调试时,我知道firstText没有调用addTextChangedListener。
以下是我的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);
正如我可以看到我在EditText上调用addTextChangedListener,我甚至将firstTextTW和secondTextTW正确地定义为两个EditText下面的全局变量
提前致谢
用于num键盘的setOntouchListener的编码代码
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;
}
});
答案 0 :(得分:0)
你这样做
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记得获取edittext的正确ID