我目前正在为学校开展货币转换器项目。 我有一个货币(美元,欧元等)的数组和一个有价值(6.54,9.56等)的数组。
我有两个微调器和一个EditText,你在一个微调器中选择From单元,在另一个微调器中选择To单元。然后它应该考虑editText中的金额并在textView中显示“实时”结果。如果您更改了微调器或editText,则textview中的值应该直接更新。
假设在editText中插入的值为10.0,我们希望将其从USD转换为EUR,计算结果如下:10.0 * 6.54 / 9.56 =
但我无法弄清楚如何将editText添加到计算中,现在它只将spinner1中的值添加到spinner2中的值。
这是strings.mxl
<resources>
<string name="app_name">CurrencyConverter3</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Dinner Time</string>
<string name="textView1Text">From:</string>
<string name="textView2Text">To:</string>
<string name="textView3Text">Amount:</string>
<string name="textView4Text">Amount:</string>
<string-array name="currency">
<item >Choose currency</item>
<item >AUD</item>
<item >USD</item>
<item >SEK</item>
<item >EUR</item>
<item >YEN</item>
</string-array>
<string-array name="price">
<item >0</item>
<item >6.91</item>
<item >6.54</item>
<item >1.00</item>
<item >9.56</item>
<item >0.0837</item>
</string-array>
这是mainactivity.java
package com.example.xxx.currencyconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
private String [] currency;
private ArrayAdapter<String> adapter;
private Spinner spin1, spin2;
private EditText amount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpTheSpinners();
}
private void setUpTheSpinners() {
currency = getResources().getStringArray(R.array.currency);
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, currency);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
AdapterView.OnItemSelectedListener listener = new currencySelectedListener();
spin1 = (Spinner)findViewById(R.id.spinner1);
spin1.setAdapter(adapter);
spin1.setOnItemSelectedListener(listener);
spin2 = (Spinner)findViewById(R.id.spinner2);
spin2.setAdapter(adapter);
spin2.setOnItemSelectedListener(listener);
}
private void calculateSum() {
String [] price = getResources().getStringArray(R.array.price);
int index1 = spin1.getSelectedItemPosition();
int index2 = spin2.getSelectedItemPosition();
double cost1 = Double.parseDouble( price[index1] );
double cost2 = Double.parseDouble( price[index2] );
double totalCost = cost1 + cost2;
TextView totalCostText = (TextView)findViewById(R.id.textView5);
totalCostText.setText("" + totalCost);
}
private class currencySelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
calculateSum();
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
谢谢
答案 0 :(得分:0)
将编辑文本值设置为从第4步
获取的输出private void convert() {
String [] price = getResources().getStringArray(R.array.price);
int index = spinner.getSelectedItemPosition();
double curr_ratio = Double.parseDouble(price[index] );
EditText et_value = (EditText)findViewById(R.id.edit_text);
double value = Double.parseDouble(et_value.getText().toString());
et_value.setText(String.valuOf(value*curr_ratio));
}