1

我已经设法让微调器值显示在文本框上,但我需要将这些值彼此分开。所以我必须将微调器 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;

}

谢谢你

4

2 回答 2

0

我能够解决这个问题。

我将 MyData 类中的值类型从 String 更改为 double。我在 CurrencyActivity 中创建了两个名为 valA 和 valB 的双字段。

然后在方法中,我将微调器的值分配给 val 字段,例如 valA= d.getValue();

然后我创建了一个新方法,只是做了 valA/valB;

这是有助于使其更易于理解的代码。

public class CurrencyActivity extends Activity {
double valA; 
double valB;

@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);
    items[1] = new MyData("GBP, British Pound",5);
    items[2] = new MyData("USD, US Dollar",15);

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];
        valA=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];
        valB = d.getValue();
        }
    @Override
    public void onNothingSelected(AdapterView parent){
        //Do nothing
    }
 });

public void divideVals(){

    double result = valA / valB;
    etResult.setText(String.valueOf(result));
}

希望这对其他人有所帮助,因为我花了很长时间才弄清楚这最终非常简单。

于 2013-08-09T20:30:14.057 回答
0

有一个简单的解决方案,但我认为您在这里混合了代码。
Integer.parseInt(d.getValue())会给你一个有效int的,你可以执行任何数学函数,但在你的代码中,MyData d当每个 Spinner 选择发生变化时,你会从字面上覆盖。
您可以向您的MyData类添加另一个字段,该字段将存储您想要的任何值int,然后简单地使用mydata.getVal()假设mydatais of type/class MyData

于 2013-08-08T14:26:54.240 回答