Ok,so I”m trying to make a Currency Converter between dollars and euros based on a tutorial I found on the web.The problem is that the tutorial is relying on 2 radio buttons to switch between the conversion, a reference for order stating to the program which method to call first.I want the program to make that conversion independetly from the two radio buttons and instantaneously,for instance if I write a number in the euro or dollar editText view…then clicking on the convert button will make the appropriate conversion.But I cannot because there are 2 methods and unless a have a way to display their input simultaneously it won’t work.So my question is how ca I update the two editText views simultaneously when I press the convert button?Thank you
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ConvertorActivity extends Activity {
TextView dollars;
TextView euros;
Button convert;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
dollars = (TextView)this.findViewById(R.id.dollars);
euros = (TextView)this.findViewById(R.id.euros);
convert = (Button)this.findViewById(R.id.convert);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
convertBoth();
}
});
}
public void convertBoth(){
convertDollarsToEuros();
convertEurosToDollars();
}
protected void convertDollarsToEuros() {
double val = Double.parseDouble(dollars.getText().toString());
// in a real app, we'd get this off the 'net
euros.setText(Double.toString(val*0.67));
}
protected void convertEurosToDollars() {
double val = Double.parseDouble(euros.getText().toString());
// in a real app, we'd get this off the 'net
dollars.setText(Double.toString(val/0.67));
}
}
I think what you want is this:
If that’s the case, you can add a member variable
mLastEditedViewIdand use aTextWatcherto track which field was last changed. Then,onClick, callconvertDollarsToEuros()orconvertEurosToDollarsaccordingly.In
onCreate:Define an inner class for the
TextWatcher: