gpt4 book ai didi

java - Android App addTextChangedListner

转载 作者:行者123 更新时间:2023-11-30 01:51:22 24 4
gpt4 key购买 nike

我的代码有问题,当我有超过 1 个 textChangeListener 时,它会崩溃。我认为问题在于多次使用“new TextWatcher”,但我不知道将其更改为什么。

我才刚刚开始学习java和app开发,所以代码和变量有点乱。

代码:

editMiles.addTextChangedListener(new TextWatcher() {
@Override

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

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
strMiles = 0 + editMiles.getText().toString();
intMiles = Integer.parseInt(strMiles);
editKnots.setText("" + intMiles * 1.15078);
editKilometers.setText("" + intMiles * 1.852);
}

@Override
public void afterTextChanged(Editable s) {

}
});

editBeaufort.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
strBeaufort = 0 + editBeaufort.getText().toString();
intBeaufort = Integer.parseInt(strBeaufort);
editKnots.setText("");
editKilometers.setText("");
editMiles.setText("");
}

@Override
public void afterTextChanged(Editable s) {

}
});
}

最佳答案

您必须从 EditText 的 onTextChanged 参数中获取输入(CharSequence s)。因此您的代码将是..

editMiles.addTextChangedListener(new TextWatcher() {
@Override

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

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
strMiles = s.toString().trim();
intMiles = Integer.parseInt(strMiles);
editKnots.setText("" + intMiles * 1.15078);
editKilometers.setText("" + intMiles * 1.852);
}

@Override
public void afterTextChanged(Editable s) {

}
});

editBeaufort.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
strBeaufort = s.toString().trim();
intBeaufort = Integer.parseInt(strBeaufort);
editKnots.setText("");
editKilometers.setText("");
editMiles.setText("");
}

@Override
public void afterTextChanged(Editable s) {

}
});
}

希望对你有用你必须为你的计算提供正确的输入,如果你想输入 int,你必须将字符串解析为 int,或者如果你想输入为 double,将字符串解析为 double 并抛出异常

关于java - Android App addTextChangedListner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com