我已经设法让微调器值显示在文本框上,但我需要将这些值彼此分开。所以我必须将微调器 1 的值除以微调器 2 的值。但是我不确定如何执行此操作。
这是我的代码:
public class CurrencyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency);
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final EditText etResult = (EditText) findViewById(R.id.etResult);
final MyData items[] = new MyData[3];
items[0] = new MyData("EUR, Euro","1.00");
items[1] = new MyData("GBP, British Pound","5.00");
items[2] = new MyData("USD, US Dollar","15.00");
ArrayAdapter<MyData> adapter = new ArrayAdapter <MyData>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
MyData d = items[pos];
etResult.setText(d.getValue());
}
@Override
public void onNothingSelected(AdapterView parent){
//Do nothing
}
});
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
MyData d = items[pos];
etResult.setText(d.getValue());
}
@Override
public void onNothingSelected(AdapterView parent){
//Do nothing
}
});
我创建了一个具有这些方法的 MyData 类
public class MyData {
public MyData(String spinnerText, String value){
this.spinnerText = spinnerText;
this.value = value;
}
public String getSpinnerText(){
return spinnerText;
}
public String getValue(){
return value;
}
public String toString(){
return spinnerText;
}
String spinnerText;
String value;
}
谢谢你