示例:如何以编程方式从 MonoDroid / C# 中的数组填充 Android Spinner

发布于 2025-01-08 11:26:08 字数 1908 浏览 3 评论 0原文

这周我开始摆弄 MonoDroid。并不是说我不喜欢 Java,而是我大部分时间都花在 Visual Studio / C# 或 Delphi 上,而且我宁愿拔掉我的指甲,也不愿学习另一个 IDE 和另一个编辑器来使用 Eclipse。

我将此代码从 Java 改编为 C#,适用于使用 C# 代码(而不是 AXML)以编程方式填充微调器的情况。也许你们中的一些人可能会发现它很有帮助。

在 Activity 的 axml 中创建通用 Spinner:

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/spinner_prompt"
/>

在适当的位置构建字符串数组:

public static readonly string[] refresh_ratesS = {"10 seconds","20 seconds","30 seconds","60 seconds","5 minutes"}; 

我更喜欢在 Activity 的公共类中实例化对象,这样我只需识别它们一次:

public class ActivityChooseRefreshRate :Activity {
    Spinner         jb_spinner;

在 Activity 的 OnCreate 函数中将数组分配给 spinner:

     //identify the spinner
jb_spinner      =   FindViewById<Spinner>(Resource.Id.spinner);
     //create the handler for when the user selects something
jb_spinner.ItemSelected += new EventHandler<ItemEventArgs> (jb_spinner_ItemSelected);
    //assign your array to an ArrayAdapter
ArrayAdapter<string>  spinnerArrayAdapter   =   new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem,refresh_ratesS);
      //Use the ArrayAdapter you've set up to populate your spinner
spinnerArrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerItem);
jb_spinner.Adapter      =   spinnerArrayAdapter;
   //optionally pre-set jb_spinner to an index
jb_spinner.SetSelection(2);

最后,对 jb_spinner_ItemSelected 处理程序进行编码,以便在用户进行选择时发生某些事情:

private void jb_spinner_ItemSelected (object sender, ItemEventArgs e){
    Spinner spinner     = (Spinner)sender;
         //do something here, such as raise a Toast
    Toast.MakeText(this, "Index="+e.Position.ToString(),ToastLength.Long).Show();
}

I started fiddling around with MonoDroid this week. Not that I don't appreciate Java, but I spend most of my time in either Visual Studio / C# or Delphi, and I'd rather pull my fingernails out than learn YET ANOTHER IDE and YET ANOTHER editor in order to use Eclipse.

I adapted this code to C# from Java, for situations where it's easier to populate my spinner programmatically in C# code rather than in the AXML. Perhaps some of you may find it helpful.

Create a generic Spinner In Your Activity's axml:

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/spinner_prompt"
/>

Build your array of string somewhere appropriate:

public static readonly string[] refresh_ratesS = {"10 seconds","20 seconds","30 seconds","60 seconds","5 minutes"}; 

I prefer to instantiate objects in the Activity's public class so that I only have to identify them once:

public class ActivityChooseRefreshRate :Activity {
    Spinner         jb_spinner;

In the Activity's OnCreate function assign your array to the spinner:

     //identify the spinner
jb_spinner      =   FindViewById<Spinner>(Resource.Id.spinner);
     //create the handler for when the user selects something
jb_spinner.ItemSelected += new EventHandler<ItemEventArgs> (jb_spinner_ItemSelected);
    //assign your array to an ArrayAdapter
ArrayAdapter<string>  spinnerArrayAdapter   =   new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem,refresh_ratesS);
      //Use the ArrayAdapter you've set up to populate your spinner
spinnerArrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerItem);
jb_spinner.Adapter      =   spinnerArrayAdapter;
   //optionally pre-set jb_spinner to an index
jb_spinner.SetSelection(2);

Lastly, code the jb_spinner_ItemSelected handler so that something happens when the user makes a selection:

private void jb_spinner_ItemSelected (object sender, ItemEventArgs e){
    Spinner spinner     = (Spinner)sender;
         //do something here, such as raise a Toast
    Toast.MakeText(this, "Index="+e.Position.ToString(),ToastLength.Long).Show();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文