Spinner的表现形式相当于C#的ComboBox,样子如下图:

第三课 Spinner的使用-LMLPHP

但选择项的添加方式相当不一样,必须使用数据适配器,上例子。

Layout——Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner1" />
</LinearLayout>

Activity1.cs

using System;
 
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Database;
using Android.Provider;
 
namespace SpinnerSample
{
    [Activity(Label = "SpinnerSample", MainLauncher = true, Icon = "@drawable/icon")]
    public class SpinnerSample : Activity
    {
        private ICursor _BookmarkCursor;
        public ICursor BookmarkCursor
        {
            get
            {
                if (_BookmarkCursor == null)
                {
                    _BookmarkCursor = GetBookmarkCursor();
                }
                return _BookmarkCursor;
            }
            set { _BookmarkCursor = value; }
        }
 
        public ICursor GetBookmarkCursor()
        {
            return ManagedQuery(Browser.BookmarksUri,
                new string[] { Browser.BookmarkColumns.Title, Browser.BookmarkColumns.Url, Browser.BookmarkColumns.InterfaceConsts.Id },
                null,
                null,
                null);
        }
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            SetContentView(Resource.Layout.Main);
 
            CreateSpinner();
        }
        
        int LastSpinnerSelectedPosition;
        public void CreateSpinner()
        {
            LastSpinnerSelectedPosition = 0;
            var TargetSpinner = FindViewById<Spinner>(Resource.Id.spinner1);
            SimpleCursorAdapter SpinnerAdapter = new SimpleCursorAdapter(this,
                Android.Resource.Layout.SimpleSpinnerItem, BookmarkCursor,
                new string[] { Browser.BookmarkColumns.Title },
                new int[] { Android.Resource.Id.Text1 });
            SpinnerAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            TargetSpinner.Adapter = SpinnerAdapter;
            TargetSpinner.Prompt = "Select ...";
 
            TargetSpinner.ItemSelected += TargetSpinner_ItemSelected;
 
        }
 
        void TargetSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            var CurSpinner = (Spinner)sender;
            var CurSelectedIndex = CurSpinner.SelectedItemPosition;
            if (LastSpinnerSelectedPosition != CurSelectedIndex)
            {
                ICursor SelectedItem = (ICursor)CurSpinner.SelectedItem;
                var UrlColumnIndex = SelectedItem.GetColumnIndex(Browser.BookmarkColumns.Url);
                TextView tv = FindViewById<TextView>(Resource.Id.textView1);
                tv.Text = SelectedItem.GetString(UrlColumnIndex);
                LastSpinnerSelectedPosition = CurSelectedIndex;
            }
        }
    }
}
 

运行此程序需要READ_HISTORY_BOOKMARKS权限,设置方式如下图。

第三课 Spinner的使用-LMLPHP

05-11 11:21