Conversion Application - for android
Im trying to create an currency conversion app but nothing happens when i click on the convert button. Here is the code i followed the tutorial.
package com.currencyconverter;
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 currencyconverter extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
Button convert;
@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);
dtoe = (RadioButton)this.findViewById(R.id.dtoe);
dtoe.setChecked(true);
etod = (RadioButton)this.findViewById(R.id.etod);
convert = (Button)this.findViewById(R.id.Convert);
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
if (dtoe.isChecked()) {
convertDollarsToEuros();
}
if (etod.isChecked()) {
convertEurosToDollars();
}
}
// TODO Auto-generated method stub
protected void convertEurosToDollars(){
double val = Double.parseDouble(euros.getText().toString());
dollars.setText(Double.t开发者_如何学编程oString(val/0.67));
}
protected void convertDollarsToEuros(){
double val = Double.parseDouble(dollars.getText().toString());
euros.setText(Double.toString(val*0.67));
}
}
Change
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
to
convert.setOnClickListener(this);
Why? If you can't answer this question you need to study more Java....
Just kidding... the reason is that you are setting an empty click listener. On the other hand, your Activity
implements the OnClickListener
interface and the onClick
method of that implementation is what you want to execute (not the empty you set to the button).
精彩评论