Android:为什么我无法更改 EditText 相对于另一个 EditText 的值?
我将根据在另一个编辑文本上输入的值来更改编辑文本的文本。也像同样的事情,反之亦然。
为此,我使用了 TextChanged Listener 并按如下方式实现:
includedText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(includedText.getText().toString().equals("")))
{
double included = Double.parseDouble(includedText.getText().toString());
included = roundTwoDecimals(included);
String amt = String.valueOf(roundTwoDecimals(included-(included/1.15)));
String excluded = String.valueOf(included/1.15);
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
excludedText.setText(excluded); //////// Error Line
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
// worked
excludedText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(excludedText.getText().toString().equals("")))
{
double excluded = Double.parseDouble(excludedText.getText().toString());
excluded = roundTwoDecimals(excluded);
String amt = String.valueOf(roundTwoDecimals(excluded*0.15));
String included = String.valueOf(roundTwoDecimals(excluded+(excluded*0.15)));
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
includedText.setText(included);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
但是执行此操作后,我无法获取文本更改。对于 Secod EditText 来说,它工作得很好,但是当我使用 First EditText 时,它在注释行处给了我错误。
错误日志:
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): FATAL EXCEPTION: main
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): java.lang.StackOverflowError
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.TextUtils.getChars(TextUtils.java:69)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.TextUtils.indexOf(TextUtils.java:102)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.StaticLayout.generate(StaticLayout.java:131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.makeNewLayout(TextView.java:4851)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.checkForRelayout(TextView.java:5348)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2688)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTAct
那么这有什么问题吗?为什么我无法同时使 EditText 生效? 谢谢。
I am going to change the text of the edittext based on the value enter on the another edittext. and also like same thig with visa-versa.
For that i have use the TextChanged Listener and implemented as like below:
includedText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(includedText.getText().toString().equals("")))
{
double included = Double.parseDouble(includedText.getText().toString());
included = roundTwoDecimals(included);
String amt = String.valueOf(roundTwoDecimals(included-(included/1.15)));
String excluded = String.valueOf(included/1.15);
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
excludedText.setText(excluded); //////// Error Line
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
// worked
excludedText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(excludedText.getText().toString().equals("")))
{
double excluded = Double.parseDouble(excludedText.getText().toString());
excluded = roundTwoDecimals(excluded);
String amt = String.valueOf(roundTwoDecimals(excluded*0.15));
String included = String.valueOf(roundTwoDecimals(excluded+(excluded*0.15)));
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
includedText.setText(included);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
But after doing this i am not able to get the text change. for Secod EditText its worked nice but while i use the First EditText then it gives me error at the commented line.
Error Log:
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): FATAL EXCEPTION: main
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): java.lang.StackOverflowError
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.TextUtils.getChars(TextUtils.java:69)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.TextUtils.indexOf(TextUtils.java:102)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.StaticLayout.generate(StaticLayout.java:131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.makeNewLayout(TextView.java:4851)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.checkForRelayout(TextView.java:5348)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2688)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): at com.project.TaxCalculator.GSTAct
So whats the wrong with this ? Why i am not able to get effect for both the EditText ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为您正在 onTextChanged 内执行 setText() 。因此,当您更改文本时,将调用 onTextChanged,并在 onTextChanged 中再次调用 setText()。因此,文本再次更改,onTextChanged() 将再次调用,此过程将继续,直到堆栈溢出并导致 stackOverFlow 错误。希望您能理解...
在setText()之前使用removeTextChangedListener()。
编辑:
定义
TextWatcher exceptTW,includeTW;
//全局。this is because you are doing setText() inside onTextChanged . So when you change the text onTextChanged will be called and again inside onTextChanged you are calling setText(). So thr text changed again and onTextChanged() will be called again and this process will continue till stack overflow and result to stackOverFlow error. Hope you can understand...
user removeTextChangedListener() before setText().
Edit:
Define
TextWatcher excludeTW,includeTW;
//globaly.我认为这给你带来了问题,因为你将整个代码放入递归中。即第一个编辑文本更改了第二个编辑文本的文本,第二个编辑文本再次更改了第一个编辑文本的文本...这永远持续下去..
解决方案:应用一些逻辑,当第一个编辑文本失去焦点时添加文本更改侦听器,反之亦然。
如果您需要帮助,请评论。
这肯定会起作用。
I think this is causing you problem because you are putting the whole code in recursion. i.e 1st editext changes the text of 2nd edittext which again changes the text of 1st edittext...this goes on forever..
Solution: apply some logic that adds the text change listener when the 1st edit text has lost its focus n vice versa.
Comment if u need help.
This will definately work.
您似乎已经将无限递归与编辑文本连接起来。
includedText
的 onTextChanged() 调用excludedText.setText()
,这会触发excludedText
上的 onTextChanged,后者再次调用includedText.setText()< /代码>。这种情况会永远重复,直到崩溃。
因此出现了 Stackoverflow。 请参阅此处的解决方案
编辑:我还没有没有编译它,只是按原样编辑它。请参阅此处
You seem to have hooked up an infinite recursion with both the edit text.
onTextChanged() of
includedText
callsexcludedText.setText()
which triggers the onTextChanged onexcludedText
which again callsincludedText.setText()
. And this repeats on forever until it crashes.Hence the Stackoverflow. See here for solution
Edit: I haven't compiled it, just edited it as is. See here
下面的代码将 100% 工作尝试它声明全局 TextWatcher t1,t2;
below code will work 100 percent try it declare globaly TextWatcher t1,t2;