Android EditText 字段和 TextWatcher

Android EditText fields and TextWatcher

我需要帮助。我不知道如何为可能的 EditText 字段执行 TextWatcher。我想这样做:如果我在第一个字段中输入文本,我想计算其他字段的值。四个字段是可编辑的,因此如果我将值赋给一个 EditText,它会更改其他字段的值,并导致循环和 Whosebugerror。

它的源代码:

package com.example.hp.ptcalculator;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class Pt100Fragment extends Fragment{
    EditText Pt100Res, Pt100TempC, Pt100TempF, Pt100TempK, Pt100Sens, Pt100TolAA, Pt100TolA, Pt100TolB, Pt100TolC;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.pt100form, container, false);
        Pt100Sens = ((EditText) view.findViewById(R.id.Pt100SensEditText));
        Pt100TolAA = ((EditText) view.findViewById(R.id.Pt100TolAAEditText));
        Pt100TolA = ((EditText) view.findViewById(R.id.Pt100TolAEditText));
        Pt100TolB = ((EditText) view.findViewById(R.id.Pt100TolBEditText));
        Pt100TolC = ((EditText) view.findViewById(R.id.Pt100TolCEditText));
        Pt100Res = ((EditText) view.findViewById(R.id.Pt100ResEditText));
        Pt100TempC = ((EditText) view.findViewById(R.id.Pt100TempCEditText));
        Pt100TempF = ((EditText) view.findViewById(R.id.Pt100TempFEditText));
        Pt100TempK = ((EditText) view.findViewById(R.id.Pt100TempKEditText));
        Pt100Res.addTextChangedListener(watcher);
        Pt100TempC.addTextChangedListener(watcher);


        return view;
    }


    TextWatcher watcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };




        public void calc1() {
            double R;
            double TC, TF, TK, Sens, TolAA, TolA, TolB, TolC;
            String tmp = Pt100Res.getText().toString();
            if (tmp.equals(""))
                R = 0;
            else
                R = Double.parseDouble(tmp);
            TC = res2temp(R);
            TF = 9 * TC / 5 + 32;
            TK = TC + 273.15;
            Sens = 0.2 / (res2temp(R * 1 + 0.1) - res2temp(R * 1 - 0.1));
            TolAA = 0.1 + 0.0017 * Math.abs(TC);
            TolA = 0.15 + 0.002 * Math.abs(TC);
            TolB = 0.30 + 0.005 * Math.abs(TC);
            TolC = 0.60 + 0.01 * Math.abs(TC);
            Pt100TempC.setText(String.valueOf(TC));
            Pt100TempF.setText(String.valueOf(TF));
            Pt100TempK.setText(String.valueOf(TK));
            Pt100Sens.setText(String.valueOf(Sens));
            Pt100TolAA.setText(String.valueOf(TolAA));
            Pt100TolA.setText(String.valueOf(TolA));
            Pt100TolB.setText(String.valueOf(TolB));
            Pt100TolC.setText(String.valueOf(TolC));
        }

        public void calc2() {
            double TC, TF, TK, Sens, TolAA, TolA, TolB, TolC, R;
            String tmp = Pt100TempC.getText().toString();
            if (tmp.equals(""))
                TC = 0;
            else
                TC = Double.parseDouble(tmp);
            R = temp2res(TC);
            TF = 9 * TC / 5 + 32;
            TK = TC + 273.15;
            Sens = 0.2 / (res2temp(R + 0.1) - res2temp(R - 0.1));
            TolAA = 0.1 + 0.0017 * Math.abs(TC);
            TolA = 0.15 + 0.002 * Math.abs(TC);
            TolB = 0.30 + 0.005 * Math.abs(TC);
            TolC = 0.60 + 0.01 * Math.abs(TC);
            Pt100Res.setText(String.valueOf(R));
            Pt100TempF.setText(String.valueOf(TF));
            Pt100TempK.setText(String.valueOf(TK));
            Pt100Sens.setText(String.valueOf(Sens));
            Pt100TolAA.setText(String.valueOf(TolAA));
            Pt100TolA.setText(String.valueOf(TolA));
            Pt100TolB.setText(String.valueOf(TolB));
            Pt100TolC.setText(String.valueOf(TolC));
        }

        public double temp2res(double temp) {
            double res;
            double coef_a = 3.90830e-3;
            double coef_b = -5.77500e-7;
            double coef_c = -4.18301e-12;
            double r0 = 100;
            if (temp > 0) {
                res = r0 * (1 + coef_a * temp + coef_b * Math.pow(temp, 2));
            } else if (temp < 0) {
                res = r0 * (1 + coef_a * temp + coef_b * Math.pow(temp, 2) + coef_c * (Math.pow(temp, 4) - 100 * Math.pow(temp, 3)));
            } else res = r0;
            return res;
        }

        public double res2temp(double res) {
            double coef_a = 3.90830e-3;
            double coef_b = -5.77500e-7;
            double coef_c = -4.18301e-12;
            double r0 = 100;
            double t, t0, Ohm2Temp;
            if (res < r0) {
                double pente = 0.38;
                double epsilon = 0.000000000001;
                int MaxIter = 100;
                t0 = -(r0 - res) / pente;
                for (int i = 1; i < MaxIter; i++) {
                    t = t0 - CVD(t0, res, r0) / CVDprime(t0, r0);
                    if (Math.abs(t - t0) <= epsilon) {
                        Ohm2Temp = t;
                        return Ohm2Temp;
                    } else t0 = t;
                }
                Ohm2Temp = 9999999;
            } else
                Ohm2Temp = (-coef_a + Math.sqrt(Math.pow(coef_a, 2) - 4 * coef_b * (1 - res / r0))) / (2 * coef_b);

            return Ohm2Temp;
        }

        public double CVD(double t, double Rt, double r0) {
            double coef_a = 3.90830e-3;
            double coef_b = -5.77500e-7;
            double coef_c = -4.18301e-12;
            double ff = r0 * (1 + coef_a * t + coef_b * Math.pow(t, 2) + coef_c * (t - 100) * Math.pow(t, 3)) - Rt;
            return ff;
        }

        public double CVDprime(double t, double R0) {
            double coef_a = 3.90830e-3;
            double coef_b = -5.77500e-7;
            double coef_c = -4.18301e-12;
            double fp = R0 * (coef_a + 2 * coef_b * t - 300 * coef_c * Math.pow(t, 2) + 4 * coef_c * Math.pow(t, 3));
            return fp;
        }


    }

更新

好的。我做了这个:

TextWatcher watcher1 = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                calc1();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Pt100TempC.removeTextChangedListener(watcher2);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Pt100TempC.addTextChangedListener(watcher2);
            }
        };
        TextWatcher watcher2 = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                calc2();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Pt100Res.removeTextChangedListener(watcher1);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Pt100Res.addTextChangedListener(watcher1);
            }
        };

但它会导致 java.lang.IllegalStateException:无法在 activity class com.example.hp.ptcalculator.MainActivity 中为视图 class 上的 onClick 处理程序找到方法 true(View) android.widget.EditText with id 'Pt100TempCEditText' 并且由以下原因引起:java.lang.NoSuchMethodException:true [class android.view.View]

要解决此问题,您将需要 4 个 TextWatcher - 每个 EditText 一个。

然后在每个 TextWatcher 中,当您要执行计算时,从所有 EditText 中删除 TextWatcher,执行您的计算,然后将它们添加回去。

这听起来开销很大,但用户根本不会注意到它。

是的,您可以从当前正在编辑的 TextView 中删除一个 TextWatcher 并将其添加回来,但您可能只想删除所有 "other" TextWatcher来自另一个 EditText 对象的对象 - 更难实现,但在技术上是正确的(正确处理快速事件)并且更容易阅读(因为你没有编辑当前的 EditText - 只有其他的)。

要使用问题和答案中的代码:

EditText Pt100Res = ((EditText) view.findViewById(R.id.Pt100ResEditText));
EditText Pt100TempC = ((EditText) view.findViewById(R.id.Pt100TempCEditText));
EditText Pt100TempF = ((EditText) view.findViewById(R.id.Pt100TempFEditText));
EditText Pt100TempK = ((EditText) view.findViewById(R.id.Pt100TempKEditText));
TextWatcher watcherRes;
TextWatcher watcherC;
TextWatcher watcherF;
TextWatcher watcherK;
watcherRes = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Pt100TempRes.removeTextChangedListener(watcherRes);
            Pt100TempC.removeTextChangedListener(watcherC);
            Pt100TempF.removeTextChangedListener(watcherF);
            Pt100TempK.removeTextChangedListener(watcherK);
            calc1();
            Pt100TempRes.addTextChangedListener(watcherRes);
            Pt100TempC.addTextChangedListener(watcherC);
            Pt100TempF.addTextChangedListener(watcherF);
            Pt100TempK.addTextChangedListener(watcherK);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    watcherC = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Pt100TempRes.removeTextChangedListener(watcherRes);
            Pt100TempC.removeTextChangedListener(watcher);
            Pt100TempF.removeTextChangedListener(watcherF);
            Pt100TempK.removeTextChangedListener(watcherK);
            calc2();
            Pt100TempRes.addTextChangedListener(watcherRes);
            Pt100TempC.addTextChangedListener(watcherC);
            Pt100TempF.addTextChangedListener(watcherF);
            Pt100TempK.addTextChangedListener(watcherK);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    watcherF = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Pt100TempRes.removeTextChangedListener(watcherRes);
            Pt100TempC.removeTextChangedListener(watcher);
            Pt100TempF.removeTextChangedListener(watcherF);
            Pt100TempK.removeTextChangedListener(watcherK);
            calc2();
            Pt100TempRes.addTextChangedListener(watcherRes);
            Pt100TempC.addTextChangedListener(watcherC);
            Pt100TempF.addTextChangedListener(watcherF);
            Pt100TempK.addTextChangedListener(watcherK);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    watcherK = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Pt100TempRes.removeTextChangedListener(watcherRes);
            Pt100TempC.removeTextChangedListener(watcher);
            Pt100TempF.removeTextChangedListener(watcherF);
            Pt100TempK.removeTextChangedListener(watcherK);
            calc2();
            Pt100TempRes.addTextChangedListener(watcherRes);
            Pt100TempC.addTextChangedListener(watcherC);
            Pt100TempF.addTextChangedListener(watcherF);
            Pt100TempK.addTextChangedListener(watcherK);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    Pt100Res.addTextChangedListener(watcherRes);
    Pt100TempC.addTextChangedListener(watcherC);
    Pt100TempF.addTextChangedListener(watcherF);
    Pt100TempK.addTextChangedListener(watcherK);

注意:您必须删除任何更改的侦听器。所以如果 calc1 没有改变 Pt100Res 那么你不需要做 remove/add 行。我没有测试这段代码,我让它有点通用(意思是我没有试图弄清楚 "calc" 函数实际上做了什么。