只是想知道我是否可以找到如何在微调器中选择项目并将其存储在一个字符串中,我已经看到了其他关于此的帖子,人们说要把这行放到代码中: (在我发布的代码的最后一行下面)
String Genders = Gender.getSelectedItem().toString();
我尝试但它给了我一条红线(getselecteditem()) - 说spinner不包含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);
非常感谢您的帮助! :)
答案 0 :(得分:3)
您可能需要实现选择句柄(ItemSelected
),例如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
。
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 ();
}