我有3个edittext,第一个用于输入价格,第二个用于输入百分比,第三个用于输出它们。例如: 第一个输入是100000,第二个输入是30,然后第三个edittext将自动填充30000。是否可以这样做?
提前感谢,
的问候, 乔治
答案 0 :(得分:2)
有可能。您可以使用editText.addTextChangedListener(观察者)。
<强> EDIT1 强>
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text1 = editText1.getText().toString();
if (text1.length() != 0) {
int input1 = Integer.valueOf(text1);
int input2 = Integer.valueOf(s.toString());
int output = (input1 * input2) / 100;
editText3.setText(output + "");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:0)
edittext.addTextChangedListener(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) {
//'calculatedPercentage' is the calculated value
editText3.setText(calculatedPercentage);
}
});
答案 2 :(得分:0)
您可以在EditText中使用TextWatcher,并使用onTextChanged方法进行计算。
答案 3 :(得分:0)
像这样设置TextWatcher:
EditText editText = (EditText) findViewById(R.id.edt1);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});