我想在微调器中选择一个项目,然后将其写入TextView
,但我有错误。
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
TextView mytext = FindViewById<TextView>(Resource.Id.textView1);
List<string> dataList = new List<string>();
dataList.Add("2");
dataList.Add("1");
dataList.Add("3");
var ArrayAdapter1 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, dataList);
spinner.Adapter = ArrayAdapter1;
if (spinner.SelectedItem.Equals("2"))
mytext.Text = "click 2";
if (spinner.SelectedItem.Equals("1"))
mytext.Text = "click 1";
答案 0 :(得分:3)
您需要订阅ItemSelected
事件并验证所选项目。
试试这个:
spinner.ItemSelected += (sender, e) => {
var itemSelected = (string) spinner.SelectedItem;
if (itemSelected == "1")
{
textView.Text = "Clicked 1";
}
else if (itemSelected == "2")
{
textView.Text = "Clicked 2";
}
};
<强>更新强>
要使用的是按钮点击事件处理程序方法,您只需:
让你的textView和你的微调器成为一个私有字段,所以可以从另一个地方访问它是你的代码,并在你的方法中添加以下代码:
var itemSelected = (string) spinner.SelectedItem;
if (itemSelected == "1")
{
textView.Text = "Clicked 1";
}
else if (itemSelected == "2")
{
textView.Text = "Clicked 2";
}
希望这会有所帮助.-