Mono for Android 篇二 使用Spinner 实现下拉列表读取Browser.BookmarksUri
http://www.cnblogs.com/ivanyb/archive/2013/03/05/2944818.html
1、首先在VS2010里面創建一個Android Application
簡單說明一下解決方案中各個文件的作用:
AndroidManifest.xml:項目環境配置文件,指明了使用Android SDK的版本,和應用能夠訪問Android 系統的權限配置
Main.axml:項目布局和控件管理文件
Strings.xml:資源文件,控件上的文本顯示可以通過 @string/Hello"這種方式獲取相應內容
Resource.Designer.cs:里面的內容會根據Main.axml中的控件生成對應的ID等信息。不需要手工編輯它。
?
2、創建應用
先上效果圖在腦海里有個影響到底創建一個什么東東
?
打開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的內容
打開模擬器后,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、在項目屬性中設置?的時候,由于android-sdk\platforms中只安裝了android-8 ,但是在設置的時候?指向到16了,報android-16找不到,所以在選擇Target API的時候請注意你android-sdk\platforms目錄中安裝了哪些文件。
轉載于:https://www.cnblogs.com/CharlesGrant/p/3662810.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Mono for Android 篇二 使用Spinner 实现下拉列表读取Browser.BookmarksUri的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android listview使用自定
- 下一篇: 资格赛:题目1:同构