zoukankan      html  css  js  c++  java
  • Mono for Android 篇二 使用Spinner 实现下拉列表读取Browser.BookmarksUri

    http://www.cnblogs.com/ivanyb/archive/2013/03/05/2944818.html

    1、首先在VS2010里面创建一个Android Application

    image

    简单说明一下解决方案中各个文件的作用:

    AndroidManifest.xml:项目环境配置文件,指明了使用Android SDK的版本,和应用能够访问Android 系统的权限配置

    Main.axml:项目布局和控件管理文件

    Strings.xml:资源文件,控件上的文本显示可以通过 @string/Hello"这种方式获取相应内容

    image

    image

    Resource.Designer.cs:里面的内容会根据Main.axml中的控件生成对应的ID等信息。不需要手工编辑它。

    2、创建应用

    先上效果图在脑海里有个影响到底创建一个什么东东

    image

    打开Main.axml 出现Design界面,项目默认创建了一个button,删除之,拖一个Spinner控件到上面,按ctrl+shift+b编译,使我们刚拖的Spinner控件对应的信息生成到Resource.Designer.cs里

    打开 Activity1.cs 文件写入如下代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    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 AndroidApplication2
    {
        [Activity(Label = "下拉列表", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : Activity
        {
             protected override void OnCreate(Bundle bundle)
            {
               
                base.OnCreate(bundle);
     
                //设置Main为我们的布局资源
                SetContentView(Resource.Layout.Main);
                               
                CreateSpinner();
               
            }
            int lastSelected;
            public void CreateSpinner()
            {
                lastSelected = 0;
     
                //根据ID找到Spinner对象
                var tagSpinner = FindViewById<Spinner>(Resource.Id.spinner1);
                //spinner是通过adapter来绑定数据,所以我们创建一个SimpleCursorAdapter,其中数据来源于BookMarkCursor
                SimpleCursorAdapter simpadp = new SimpleCursorAdapter(this,
                    Android.Resource.Layout.SimpleSpinnerItem, BookMarkCursor,
                    new string[] { Browser.BookmarkColumns.Title },
                    new int[] { Android.Resource.Id.Text1 });
                 
                simpadp.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                //将创建的SimpleCursorAdapter 赋给Spinner的Adapter属性,完成数据绑定
                tagSpinner.Adapter = simpadp;
                tagSpinner.Prompt = "选择";
     
                //注册ItemSelected 事件,实现点击item打开对应的URL
                tagSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(tagSpinner_ItemSelected);
            }
     
            void tagSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                Spinner curspinner = (Spinner)sender;
                int curPosition=curspinner.SelectedItemPosition;
                if (lastSelected != curPosition)
                {
                    ICursor cursor = (ICursor)curspinner.SelectedItem;
                    int urlColumnIndex = cursor.GetColumnIndex(Browser.BookmarkColumns.Url);
                    string url = cursor.GetString(urlColumnIndex);
                    Intent intent = new Intent(Intent.ActionView);
                    intent.SetData(Android.Net.Uri.Parse(url));
                    StartActivity(intent);
     
                    lastSelected = curPosition;
                }
            }
     
            private ICursor _BookMarkCursor;
            public ICursor BookMarkCursor
            {
                get
                {
                    if (_BookMarkCursor == null)
                    {
                        _BookMarkCursor = GetBookMarkCursor();
                    }
                    return _BookMarkCursor;
                }
                set
                {
                    _BookMarkCursor = value;
                }
            }
     
            private ICursor GetBookMarkCursor()
            {
                return ManagedQuery(Browser.BookmarksUri, new string[]{
               Browser.BookmarkColumns.Title
               ,Browser.BookmarkColumns.Url
               ,Browser.BookmarkColumns.InterfaceConsts.Id
               }, null, null, null);
            }
        }
    }
    1
      

    在项目属性的”Android Manifest” 标签中选择勾上”READ_HISTORY_BOOKMARKS“ 以允许应用程序读取BOOKMARKS的内容

    image

    打开模拟器后,F5运行,即出现可发布应用程序到模拟器中运行.

    注意点:

    1、Main.axml 里面的Spinner 控件的android:layout_height 要设置成"wrap_content" 如果设置成"fill_parent" 会报错:当前线程不能创建子控件(大概是这个意思,文字不一定准确。。。)

    2、在创建SimpleCursorAdapter 的时候,

    SimpleCursorAdapter simpadp = new SimpleCursorAdapter(this,
                  Android.Resource.Layout.SimpleSpinnerItem, BookMarkCursor,
                  new string[] { Browser.BookmarkColumns.Title },
                  new int[] { Android.Resource.Id.
    Text2
     });

    将最后一项设置成了Text2发现,下拉列表中不出现文字,改为Android.Resource.Id.Text1子控件就正常了

    3、在项目属性中设置image 的时候,由于android-sdkplatforms中只安装了android-8 ,但是在设置image的时候image 指向到16了,报android-16找不到,所以在选择Target API的时候请注意你android-sdkplatforms目录中安装了哪些文件。

  • 相关阅读:
    start tag, end tag issues in IE7, particularly in xslt transformation
    用SandCastle为注释生成chm文档
    Firebug
    架构的重点
    Linux Shell常用技巧(十) 管道组合
    Linux JDK升级
    Linux Shell常用技巧(十二) Shell编程
    Packet Tracer 5.0实验(一) 交换机的基本配置与管理
    Linux Shell常用技巧(六) sort uniq tar split
    Linux Shell常用技巧(二) grep
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/3662810.html
Copyright © 2011-2022 走看看