如何在 Spinner 中选择项目以将其用作字符串? [英] How to get item selected in Spinner to use it as string?

查看:23
本文介绍了如何在 Spinner 中选择项目以将其用作字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想看看我是否能找出我如何在微调器中选择项目并将其存储在一个字符串中,我已经看过其他关于此的帖子,人们说将此行放入代码中:(在我在下面发布的代码的最后一行下方)

Just looking to see if I can find out how i get the item selected in the spinner and store it in a string, i have seen the other posts about this and people say to put this line into code: (Beneath the last line of the code i posted below)

String Genders = Gender.getSelectedItem().toString();

我尝试过,但它在下面给了我一条红线 (getselecteditem()) - 说 Spinner 不包含 getselecteditem() 的定义

I try but it gives me a red line underneath (getselecteditem()) - saying spinner does not contain a definition for getselecteditem()

这是我的代码:

  Spinner Gender;
  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Form);

        var Genders = new String[]
        {
            "Male", "Female"
        };
        BaseMale = 2000;
        Gender = FindViewById<Spinner>(Resource.Id.spinner1);
        Gender.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Genders);

非常感谢您的帮助!:)

Would really appreciate any of your help! :)

推荐答案

您可能需要实现选择句柄 (ItemSelected),就像在这个来自 https://developer.xamarin.com/guides/android/user_interface/spinner/:

you might need to implement the selection handle (ItemSelected) like in this example from https://developer.xamarin.com/guides/android/user_interface/spinner/:

protected override void OnCreate (Bundle bundle)
{
        base.OnCreate (bundle);

        // Set our view from the "Main" layout resource
        SetContentView (Resource.Layout.Main);

        Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner);

        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);
        var adapter = ArrayAdapter.CreateFromResource (
                this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);

        adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
        spinner.Adapter = adapter;
}

这里是句柄,所选项目的索引在此处显示为 e.Position.

and here is the handle, the index to selected item appears as e.Position here.

private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
        Spinner spinner = (Spinner)sender;

        string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
}

这篇关于如何在 Spinner 中选择项目以将其用作字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆