Dzh*_*eyt 0 android textwatcher
我正在尝试根据以下任何输入(每小时,每天,每周,每月,每年)计算此人收到的薪水。输入其中一个时,应自动重新计算其他一个。
这是我的操作方法:
首先,Double在活动顶部定义了5个类型变量。它们是:每小时,每天,每周,每月,每年。然后,我有5个EditText字段,对应于这些变量。我已经附加了一个实现TextWatcher这5个EditText 的自定义子类。
例如:
etHourly = (EditText) findViewById(R.id.etHourly);
etHourly.addTextChangedListener(new EditTextWatcher(etHourly));
Run Code Online (Sandbox Code Playgroud)
这个自定义类具有一个构造函数,该构造函数接受并存储传递给它的视图,因为TextWatcher该类的默认方法无法提供找出哪个View调用了更改的方法。
在将传递的视图保存为自定义子类中的局部变量之后,我afterTextChanged在该子类中继续实现,并获取传递的EditText的值,并将其另存为Double活动顶部的相应定义变量。(例如,如果传递的EditText用于每周薪水,则将该EditText的值设置为weekly变量的双精度值。
最后,在该afterTextChanged方法结束之前,我调用了另一个自定义方法Recalculate(),该方法具有一堆,if()用于检查是否设置了每小时,每天,每周,每月或每年,是否设置setText()了剩余的EditText。问题在于,这setText()将为每个EditText调用TextWatchers,从而导致无限循环。
我该如何克服呢?
这里有一些代码可以更好地理解这一点。在onCreate之前:
Double hourly, daily, weekly, monthly, yearly = 0.0;
EditText etHourly, etDaily, etWeekly, etMonthly, etYearly;
Run Code Online (Sandbox Code Playgroud)
在onCreate()内部:
etHourly = (EditText) findViewById(R.id.etHourly);
etDaily = (EditText) findViewById(R.id.etDaily);
etWeekly = (EditText) findViewById(R.id.etWeekly);
etMonthly = (EditText) findViewById(R.id.etMonthly);
etYearly = (EditText) findViewById(R.id.etYearly);
etHourly.addTextChangedListener(new EditTextWatcher(etHourly));
etDaily.addTextChangedListener(new EditTextWatcher(etDaily));
etWeekly.addTextChangedListener(new EditTextWatcher(etWeekly));
etMonthly.addTextChangedListener(new EditTextWatcher(etMonthly));
etYearly.addTextChangedListener(new EditTextWatcher(etYearly));
Run Code Online (Sandbox Code Playgroud)
该EditTextWatcher子类:
private class EditTextWatcher implements TextWatcher {
EditText v;
public EditTextWatcher(EditText view) {
this.v = view;
}
public void afterTextChanged(Editable s) {
Reinit();
// Only if the currently edited text field contains something
if (v.getText().toString().length() > 0) {
switch (v.getId()) {
case R.id.etHourly:
hourly = getTvAsDouble(etHourly);
break;
case R.id.etDaily:
daily = getTvAsDouble(etDaily);
break;
case R.id.etWeekly:
weekly = getTvAsDouble(etWeekly);
break;
case R.id.etMonthly:
monthly = getTvAsDouble(etMonthly);
break;
case R.id.etYearly:
yearly = getTvAsDouble(etYearly);
break;
default:
}
}
Recalculate();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
Run Code Online (Sandbox Code Playgroud)
Reinit():
hourly = daily = weekly = monthly = yearly = 0.0;
Run Code Online (Sandbox Code Playgroud)
重新计算():
if(hourly!=null && hourly>0.0){
etDaily.setText(String.valueOf(hourly*8));
}
// I will complete the other if's once this works
Run Code Online (Sandbox Code Playgroud)
伴侣!我也被困在这里....我得到了StackOverflowError ...但是我假设这可能是因为我多次实例化了TextWatcher ...但是您通过确定问题来帮助了我,这实际上是setText( ),它无限调用TextWatcher的方法....所以我在这里做到了---根据您的代码--->
if(hourly!=null && hourly>0.0){
if(etHourly.isFocused()) //this condition helped suppressing infinite loops
etDaily.setText(String.valueOf(hourly*8));}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2688 次 |
| 最近记录: |