您可以通过声明私有 globar Spinner 来实现这一点,如下所示:
private Spinner spinner1;
private Spinner spinner2;
// your code
// now just use the globally declared spinners (spinner1 and spinner2) in your code
private void UnitBegin_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
string selected1 = spinner1.SelectedItem.ToString();
string selected2 = spinner2.SelectedItem.ToString();
if(selected1.Equals("Yard²"))
{
if (selected2.Equals("m²"))
{
//do calculation
}
else if (selected2.Equals("unit²"))
{
//do calculation
}
else if (selected2.Equals("otherunit²"))
{
//do calculation
}
else
{
//ERROR
}
}
if (selected1.Equals("m²"))
{
if (selected2.Equals("Yard²"))
{
//do calculation
}
else if (selected2.Equals("unit²"))
{
//do calculation
}
else if (selected2.Equals("otherunit²"))
{
//do calculation
}
else
{
//ERROR
}
}
//repeat for all the possible units
}
但我建议你做这样的事情:
private Spinner spinner1;
private Spinner spinner2;
private float unit1;
private float unit2;
// your code
// now just use the globally declared spinners (spinner1 and spinner2) in your code
private void UnitBegin_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
string selected1 = spinner1.SelectedItem.ToString();
if(selected1.Equals("Yard²"))
{
unit1 = 0.5 //let's say this is the multiplier from Yard² to m² :)
}
//repeat for all the possible units
}
private void UnitEnd_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
string selected2 = spinner2.SelectedItem.ToString();
if(selected2.Equals("UNIT²"))
{
unit2 = 0.7 //let's say this is the multiplier from UNIT² to m² :)
}
//repeat for all the possible units
}
并在计算结果显示的方法中:
float result = value*unit1; //and you get the value in the "default" unit
float finalResult = result*unit2; //and you get the value converted to the final unit
return finalResult; //return the final converted value