ある単位を別の単位 (通貨など) に変換するアプリを作成しようとしています。したがって、2 つの編集テキストで構成されます。1 つはユーザーが値を入力するもので、2 番目は結果を含むものです。さて、ここでは、2 番目の編集テキストに値を入力するための「変換」ボタンを使用する代わりに、最初のテキストに値を入力するときに、変換された値を 2 番目の編集テキストに表示したいと考えています。どうすればこれを達成できますか? ありがとう
4431 次
1 に答える
3
これには a を使用TextWatcher
します。EditText
ユーザーが入力したものに設定します。
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
});
編集1:
空の文字列を確認します""
:
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
if (value.equals("")) {
myEditText1.setText("0");
// You may not need this line, because "myEditText1.setText("0")" will
// trigger this method again and go to else block, where, if your code is set up
// correctly, myEditText2 will get the value 0. So, try without the next line
// and if it doesn't work, put it back.
myEditText2.setText("0");
} else {
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count){
}
});
于 2013-07-25T10:10:29.367 に答える