在我的Android应用程序中。我有一个EditText和Textview.in Edittext用户需要 输入金额。当他在编辑文本中输入金额时,在textview中它必须 显示总价值(增加税额)。怎么做。
答案 0 :(得分:5)
您可以使用textwatcher。
editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:1)
您必须为EditText添加TextWatcher作为TextChangeListener:
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
mTextView.setText(mEditText.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 2 :(得分:0)
EditText Et = (EditText) findViewById(R.id.editTextID);
Et.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// 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 onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
// You need to write your code here when user need to enter the amount.
});
答案 3 :(得分:0)
我添加了示例代码,它会持续观察edittext更改并执行一些操作,然后在textview中更新这些值。我希望这将有所帮助。 Plz遵循命名惯例。
EditText edit =(EditText)findViewById(R.id.editText1);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
TextView twxt = (TextView) findViewById(R.id.textView1);
Double sum = 0.00;
Double userValue = 0.00;
// TODO Auto-generated method stub
try {
userValue = Double.valueOf(s.toString());
} catch (Exception e) {
e.printStackTrace();
}
sum = 2 * (sum + userValue);
twxt.setText(String.valueOf(sum));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});