0
我目前正在爲學校開發貨幣換算器項目。 我有貨幣(美元,歐元等)和數值(6.54,9.56等)的陣列。用Spinner將EditText值相乘
我有兩個微調器和一個EditText,你可以選擇從一個微調器中的單元和另一個微調器中的單元。然後它應該考慮editText中的數量並在textView中顯示「實時」結果。如果你改變spinners或editText,textview中的值應該直接更新。
比方說,插在EDITTEXT值是10.0,我們希望把它從美元到歐元轉換,計算將是這樣的: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) {
}
}
}
謝謝配合「的EditText
您好,感謝爲答覆,但仍然無法正常工作。當我使用該解決方案時,應用程序崩潰 – Adde
很難預料,不會在這裏發佈錯誤日誌 – uguboz
我現在已經添加了錯誤日誌。謝謝 – Adde