每次用户更改输入时,我都会尝试重新计算,因此我使用了 TextWatcher。它会在更改文本时计算应用程序何时启动,当我输入新值时它不会重新计算。有人可以告诉我我做错了什么吗?使用“onTextChanged”是否正确,我是否必须将整个计算放在该过程中,还是只调用变量一和二变量?请忽略微调器。提前致谢。
公共类 AndroidActivity 扩展 Activity 实现 TextWatcher{
private Spinner spinner1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pounds);
addListenerOnSpinnerItemSelection();
int one = 0;
int two = 0;
try{
EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
one = Integer.valueOf(one100lbs.getText().toString().trim());
}
catch (NumberFormatException e)
{
one = -1;
}
try{
EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
two = Integer.valueOf(tenPounds.getText().toString().trim());
}
catch (NumberFormatException e)
{
two = -1;
}
int result = one + two;
TextView textView = (TextView) findViewById(R.id.editText1);
textView.setText(String.valueOf(result));
}
//create spinner
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.other_spinner);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
@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
int one = 0;
int two = 0;
try{
EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
one = Integer.valueOf(one100lbs.getText().toString().trim());
}
catch (NumberFormatException e)
{
one = -1;
}
try{
EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
two = Integer.valueOf(tenPounds.getText().toString().trim());
}
catch (NumberFormatException e)
{
two = -1;
}
int result = one + two;
TextView textView = (TextView) findViewById(R.id.editText1);
textView.setText(String.valueOf(result));
}
}