Hi All,
So I'm just working through a tutorial Application and I can't seem to get the onClick routine working for a button. I managed to get it working by other means (setting the button's onclick property to button_event and writing up a button_event method) but not by implementing the onclickListener for the activity. I have attached the code and would be so grateful if someone could point out where I went wrong.
Thanks
So I'm just working through a tutorial Application and I can't seem to get the onClick routine working for a button. I managed to get it working by other means (setting the button's onclick property to button_event and writing up a button_event method) but not by implementing the onclickListener for the activity. I have attached the code and would be so grateful if someone could point out where I went wrong.
Thanks
Code:
package com.test.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
public class mainPage extends Activity implements OnClickListener {
TextView dollars;
TextView euros;
TextView debug;
RadioButton dtoe;
RadioButton etod;
Button convert;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dollars = (TextView)this.findViewById(R.id.dollars);
euros = (TextView)this.findViewById(R.id.euros);
debug = (TextView)this.findViewById(R.id.Debug);
dtoe = (RadioButton)this.findViewById(R.id.dtoe);
etod = (RadioButton)this.findViewById(R.id.etod);
dtoe.setChecked(true);
debug.setText("Program Started..");
convert = (Button)this.findViewById(R.id.convert);
convert.setOnClickListener(this);
}
@Override
public void onClick(View v) {
debug.setText("Click handler routine");
if (dtoe.isChecked())
{
convertDollarsToEuros();
}
if (etod.isChecked())
{
convertEurosToDollars();
}
}
protected void convertDollarsToEuros()
{
double val = Double.parseDouble(dollars.getText().toString());
euros.setText(Double.toString(val*0.67));
debug.setText(euros.getText());
}
protected void convertEurosToDollars()
{
double val = Double.parseDouble(euros.getText().toString());
dollars.setText(Double.toString(val/0.67));
}
}