我需要帮助。我不知道如何为EditText字段执行TextWatcher。我想这样做:如果我在第一个字段中输入文本,我想计算其他字段的值。四个字段是可编辑的,因此如果我将值放在一个EditText中,它会更改其他值,并导致循环和Stackoverflower。
它的源代码:
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);
}
};
但它casue java.lang.IllegalStateException:在活动类com.example.hp.ptcalculator.MainActivity中找不到方法true(View)for view handler for android class.widget.EditText with id&#39 ; Pt100TempCEditText&#39;和引起:java.lang.NoSuchMethodException:true [class android.view.View]
答案 0 :(得分:0)
要解决此问题,您需要4个TextWatcher
s - 每个EditText
一个。
然后在每个TextWatcher
中,当您要执行计算时,从所有TextWatcher
中删除EditText
,执行计算,然后重新添加。
这听起来像很多开销,但它根本不应该引起用户注意。
是的,您可以从当前正在修改的TextWatcher
中删除TextView
并将其添加回来,但您可能只想删除所有其他&#34;其他&#34;来自另一个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
,则您无需执行删除/添加行。我没有对这段代码进行测试,而且我对它进行了一些通用(这意味着我并没有试图弄清楚&#34; calc&#34;函数实际上做了什么。