我正在尝试在Android中构建一个简单的转换器代码(将脚转换为mtr)。我能够这样做但只有当用户点击某个按钮时才能这样做。现在我想修改它,以便它在用户提供输入时开始转换(像谷歌转换器这样的东西)。在Android中有什么办法吗? 提前谢谢。
答案 0 :(得分:1)
将侦听器添加到您的edittext:
yourEditText.addTextChangedListener(addTextWatcher);
添加TextWatcher界面:
private TextWatcher addTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
@Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable sequence) {
}
};