我在textwatcher中使用十进制格式时遇到问题。 如果我不在TextWatcher中使用DecimalFormat,则程序中没有问题。 我可以删除并设置所有edittext(上面的代码/照片左) 但如果我使用,该程序将停止。(下面的代码/照片右) 例如,当我在第一个edittext中输入值并转到第二个edittext时,程序将停止。 我也尝试了afterTextChanged和try-catch中的代码,但没有变化。 我该如何解决这个问题?
etType5.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getCurrentFocus() == etType5) {
if (etType5.length() != 0) {
double minute = Double.parseDouble(etType5.getText().toString());
double second = minute * 60.0;
etType6.setText(String.valueOf(second));
} else {
etType6.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getCurrentFocus() == etType6) {
if (etType6.length() != 0) {
double second = Double.parseDouble(etType6.getText().toString());
double minute = second * 0.016666667;
etType5.setText(String.valueOf(minute));
} else {
etType5.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType5.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormat df = new DecimalFormat("#.###");
if (getCurrentFocus() == etType5) {
if (etType5.length() != 0) {
double minute = Double.parseDouble(etType5.getText().toString());
double second = minute * 60.0;
etType6.setText(df.format(second));
} else {
etType6.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormat df = new DecimalFormat("#.###");
if (getCurrentFocus() == etType6) {
if (etType6.length() != 0) {
double second = Double.parseDouble(etType6.getText().toString());
double minute = second * 0.016666667;
etType5.setText(df.format(minute));
} else {
etType5.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});