如何获取微调器的 ID?

How to get the ID of the spinner?

我想从微调器中获取:"ID" 所选项目。

这是我的代码:

var sterren1Lems = new String[]
         {
                "1 out of 5 stars", "2 out of 5 stars", "3 out of 5 stars", "4 out of 5 stars", "5 out of 5 stars"
         };

            sterren2Lems = FindViewById<Spinner>(Resource.Id.sterrenLems);
            sterren2Lems.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, sterren1Lems);

            TextView test = FindViewById<TextView>(Resource.Id.test);

            sterren2Lems.ItemSelected += delegate
            {
                test.Text = sterren1Lems[Convert.ToInt32(sterren2Lems.SelectedItemId)];
            };

请帮帮我!

您不必在字符串中提取任何值。您仅通过所选项目的位置就知道该值。

sterren2Lems.ItemSelected += Sterren2Lems_ItemSelected;

void Sterren2Lems_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
     test.Text = sterren1Lems[e.Position];
}

在您的情况下,这两种方法都不起作用,我通过讨论得知您可以从数组中获取单个 string。这样只需使用简单的循环即可获得 id.

sting yourString ="2 out of 5 stars";
string id = "";

for (int i = 0; i < yourString.Length; i++)
{
    if (Char.IsDigit(yourString[i]))
        result += str[i];
    else
        break;
}

string id = new String(yourString.TakeWhile(Char.IsDigit).ToArray());