我實現了一個簡單的應用程序 - 我希望它可以幫助你。
public class MainActivity extends Activity implements TextWatcher {
TextView tvResult;
EditText tvNumberOne, tvNumberTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.tvResult);
tvNumberOne = (EditText)findViewById(R.id.tvNumberOne);
tvNumberTwo = (EditText)findViewById(R.id.tvNumberTwo);
tvNumberOne.addTextChangedListener(this);
tvNumberTwo.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
int numOne = 0, numTwo = 0;
try{
numOne = Integer.valueOf(String.valueOf(tvNumberOne.getText()));
numTwo = Integer.valueOf(String.valueOf(tvNumberTwo.getText()));
}catch(Exception ex){
Toast.makeText(getApplicationContext(), "Parsing error!",Toast.LENGTH_SHORT).show();
}
tvResult.setText(String.valueOf(numOne + numTwo));
}
@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
}
}
這極大的幫助。感謝您的時間。 – 2014-10-11 21:00:32
不客氣。 – rogi1609 2014-10-11 21:07:52
只是好奇,實現它的優勢是什麼vs實例化它像這樣: TextWatcher textWatcher = new TextWatcher(){ } – 2014-10-13 16:58:27