分类:C#、Android、VS2015; 创建日期:2016-02-06 这一章主要介绍Android简单控件的基本用法。本章源程序共有9个示例,这些示例都在同一个项目中。 项目名:ch05demos,项目模板:Blank App(Android) 运行主界面截图如下: 点击每行的示例项,即进入对应示
分类:C#、Android、VS2015;
创建日期:2016-02-06
这一章主要介绍Android简单控件的基本用法。本章源程序共有9个示例,这些示例都在同一个项目中。
项目名:ch05demos,项目模板:Blank App(Android)
运行主界面截图如下:
点击每行的示例项,即进入对应示例的页面。
添加的图片见下面的左图,也可以直接拖放图片到drawable文件夹下。
右图是各节例子实现后的纵向屏幕布局文件(layout文件夹)、横向屏幕布局文件(layout-land文件夹)、弹出菜单布局文件(menu文件夹)。这些都是在本章后续将要介绍的节中添加的。
修改后的代码如下:
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ListView </span><span style="color: #ff0000;">android:minWidth</span><span style="color: #0000ff;">="25px"</span><span style="color: #ff0000;"> android:minHeight</span><span style="color: #0000ff;">="25px"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;"> android:id</span><span style="color: #0000ff;">="@+id/listView1"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span></span></span>
本章示例全部完成后MainActivity.cs的代码如下:
<span style="color: #0000ff;">using</span><span style="color: #000000;"> System; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Widget; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> ch05demos.SrcActivity; </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ch05demos { [Activity(Label </span>= <span style="color: #800000;">"</span><span style="color: #800000;">ch05demos</span><span style="color: #800000;">"</span>, MainLauncher = <span style="color: #0000ff;">true</span>, Icon = <span style="color: #800000;">"</span><span style="color: #800000;">@drawable/icon</span><span style="color: #800000;">"</span><span style="color: #000000;">)] </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MainActivity : Activity { </span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] items; </span><span style="color: #0000ff;">PRotected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle bundle) { </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(bundle); SetContentView(Resource.Layout.Main); items </span>= <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span><span style="color: #000000;">[] { </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo01EditText</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo02Login</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo03MultiResolution</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo04CheckBoxRadioButton</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo05Spinner</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo06SwitchAndRatingBar</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo07PopupMenu</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo08Gallery</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Demo09SeekBar</span><span style="color: #800000;">"</span><span style="color: #000000;"> }; ListView listView1 </span>= FindViewById<listview><span style="color: #000000;">(Resource.Id.listView1); listView1.Adapter </span>= <span style="color: #0000ff;">new</span> ArrayAdapterstring>(<span style="color: #0000ff;">this</span><span style="color: #000000;">, </span><span style="color: #000000;">Android.Resource.Layout.SimpleListItem1, items); listView1.ItemClick </span>+= (s, e) =><span style="color: #000000;"> { </span><span style="color: #0000ff;">int</span> index = e.Position + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(index) { </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">1</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo01EditText)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">2</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo02Login)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">3</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo03MultiResolution)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">4</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo04CheckBoxRadioButton)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">5</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo05Spinner)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">6</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo06SwitchAndRatingBar)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">7</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo07PopupMenu)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">8</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo08Gallery)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">case</span> <span style="color: #800080;">9</span><span style="color: #000000;">: StartActivity(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Demo09SeekBar)); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } }; } } }</span></listview>
在这个文件中只添加了一条内容:设置应用到所有页面的公用主题。
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><span style="color: #800000;">manifest </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> package</span><span style="color: #0000ff;">="ch05demos.ch05demos"</span><span style="color: #ff0000;"> android:versionCode</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> android:versionName</span><span style="color: #0000ff;">="1.0"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">uses-sdk </span><span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><span style="color: #800000;">application </span><span style="color: #ff0000;">android:label</span><span style="color: #0000ff;">="ch05demos"</span><span style="color: #ff0000;"> android:theme</span><span style="color: #0000ff;">="@android:style/Theme.DeviceDefault.Light"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">application</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">manifest</span><span style="color: #0000ff;">></span></span></span></span>
从下一节开始,将分别介绍如何实现各个示例,以及这些示例涉及的相关概念。