我有一个转换货币的安卓程序。问题是货币没有转换。这是我的代码。
package com.example.currencyconverter;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
implements OnClickListener,
OnItemSelectedListener{
EditText edittextdollars;
EditText edittexteuros;
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
RadioGroup radiogroup;
Spinner spinner1;
Spinner spinner2;
Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
calculate = (Button) findViewById(R.id.calculate);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
String text = spinner1.getSelectedItem().toString().trim();
String text2 = spinner2.getSelectedItem().toString().trim();
if (text.equalsIgnoreCase("Dollar") && text2.equalsIgnoreCase("Euro")) {
convertDollarstoEuros();
}
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
public void convertDollarstoEuros() {
double val = Double.parseDouble(edittextdollars.getText().toString());
DecimalFormat df = new DecimalFormat("###.##");
edittexteuros.setText(df.format(val*0.77));
}
protected void convertEurostoDollars() {
double val = Double.parseDouble(edittexteuros.getText().toString());
DecimalFormat df = new DecimalFormat("###.##");
edittextdollars.setText(df.format(val*1.3));
}
protected void convertEurostoEuros() {
double val = Double.parseDouble(edittexteuros.getText().toString());
DecimalFormat df = new DecimalFormat("###.##");
edittextdollars.setText(df.format(val*1));
}
protected void convertDollarstoDollars() {
double val = Double.parseDouble(edittextdollars.getText().toString());
DecimalFormat df = new DecimalFormat("###.##");
edittexteuros.setText(df.format(val*1));
}
}
toast 部分正在工作,但货币转换不起作用(请查看 onClick 函数)。我有两个旋转器。吐司只是显示,或者当我单击计算时,应用程序崩溃了。