Android 将TextWatcher类附加到多个EditText而不使用有限循环

Android 将TextWatcher类附加到多个EditText而不使用有限循环,android,textwatcher,Android,Textwatcher,我试图根据以下任何输入计算员工的工资-小时、日、周、月、年。输入其中一项后,应自动重新计算其他项 以下是我的做法: 首先,我在活动顶部定义了5个Double类型变量。它们是:每小时、每天、每周、每月、每年。然后我有5个EditText字段,对应于这些变量。我在这5个EditText中附加了一个自定义子类,它实现了TextWatcher 例如: etHourly = (EditText) findViewById(R.id.etHourly); etHourly.addTextChangedLis

我试图根据以下任何输入计算员工的工资-小时、日、周、月、年。输入其中一项后,应自动重新计算其他项

以下是我的做法:

首先,我在活动顶部定义了5个
Double
类型变量。它们是:每小时、每天、每周、每月、每年。然后我有5个EditText字段,对应于这些变量。我在这5个EditText中附加了一个自定义子类,它实现了
TextWatcher

例如:

etHourly = (EditText) findViewById(R.id.etHourly);
etHourly.addTextChangedListener(new EditTextWatcher(etHourly));
这个自定义类有一个构造函数,它接受并存储传递给它的视图,因为
TextWatcher
类的默认方法不提供找出哪个视图调用了更改的方法

将传递的视图保存为自定义子类中的局部变量后,我在该子类中继续执行已实现的
posterextchanged
,获取传递的EditText的值,并将其保存为活动顶部相应定义变量的
Double
。(例如,如果传递的EditText是针对周薪的,我将此EditText的值设置为
Weekly
变量的双精度值

最后,就在
PostTextChanged
方法结束之前,我调用了另一个自定义方法
Recalculate()
,该方法有一组
if()
,用于检查是否设置了小时、日、周、月或年,如果设置了,则计算并使用
setText()
在其余的EditText上。问题是此
settText()
将为每个EditText调用TextWatchers,从而导致无限循环

我如何克服这个问题

下面是一些代码,可以更好地理解这一点。在重新创建之前:

Double hourly, daily, weekly, monthly, yearly = 0.0;
EditText etHourly, etDaily, etWeekly, etMonthly, etYearly;
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));
内部onCreate():

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) {
    }

}
Reinit():

重新计算():


伙计!!我也被困在这里了…我得到了StackOverflower错误…但我认为可能是因为我多次实例化TextWatcher…但是你帮了我确定了问题,这实际上是与setText()有关的,这是在无限地调用TextWatcher的方法…所以我在这里这么做了---根据您的代码-->


伙计!!我也被困在这里了…我得到了StackOverflower错误…但我认为可能是因为我多次实例化TextWatcher…但是你帮了我确定了问题,这实际上是与setText()有关的,这是在无限地调用TextWatcher的方法…所以我在这里这么做了---根据您的代码-->

可能的重复可能的重复
hourly = daily = weekly = monthly = yearly = 0.0;
if(hourly!=null && hourly>0.0){
      etDaily.setText(String.valueOf(hourly*8));
}
// I will complete the other if's once this works
if(hourly!=null && hourly>0.0){
 if(etHourly.isFocused())   //this condition helped suppressing infinite loops
  etDaily.setText(String.valueOf(hourly*8));}