0

我对android中的微调器类有些困惑。我想要的是制作一个转换器,用户选择他们想要转换的单位,然后在第二个微调器中选择输出单位。Ex Spinner 1: Yard² 到 Spinner 2: Feet²。我不知道如何设置它,所以如果选择了 yard² 和 feet² 则进行此计算。这是我到目前为止的代码:

         private void UnitBegin_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)

        {
            Spinner UnitBegin = (Spinner)sender;
            string ubget = UnitBegin.SelectedItem.ToString();
            if (ubget == "Yard²")
            {
            }



        }
4

1 回答 1

0

您可以通过声明私有 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
于 2012-12-12T14:04:50.613 回答