分类:C#、Android、VS2015;

创建日期:2016-02-06

这一章主要介绍Android简单控件的基本用法。本章源程序共有9个示例,这些示例都在同一个项目中。

项目名:ch05demos,项目模板:Blank App(Android)

运行主界面截图如下:

点击每行的示例项,即进入对应示例的页面。

1、在drawable文件夹下添加图片

添加的图片见下面的左图,也可以直接拖放图片到drawable文件夹下。

右图是各节例子实现后的纵向屏幕布局文件(layout文件夹)、横向屏幕布局文件(layout-land文件夹)、弹出菜单布局文件(menu文件夹)。这些都是在本章后续将要介绍的节中添加的。

2、主界面(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">    <ListView        android:minWidth="25px"        android:minHeight="25px"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/listView1" /></LinearLayout>

3、主界面对应的活动文件(MainActivity.cs)

本章示例全部完成后MainActivity.cs的代码如下:

using System;using Android.App;using Android.Widget;using Android.OS;using ch05demos.SrcActivity;namespace ch05demos{    [Activity(Label = "ch05demos", MainLauncher = true, Icon = "@drawable/icon")]    public class MainActivity : Activity    {        string[] items;        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            SetContentView(Resource.Layout.Main);            items = new string[]            {                "Demo01EditText",                "Demo02Login",                "Demo03MultiResolution",                "Demo04CheckBoxRadioButton",                "Demo05Spinner",                "Demo06SwitchAndRatingBar",                "Demo07PopupMenu",                "Demo08Gallery",                "Demo09SeekBar"            };            ListView listView1 = FindViewById<ListView>(Resource.Id.listView1);            listView1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);            listView1.ItemClick += (s, e) =>            {                int index = e.Position + 1;                switch(index)                {                    case 1:                        StartActivity(typeof(Demo01EditText));                        break;                    case 2:                        StartActivity(typeof(Demo02Login));                        break;                    case 3:                        StartActivity(typeof(Demo03MultiResolution));                        break;                    case 4:                        StartActivity(typeof(Demo04CheckBoxRadioButton));                        break;                    case 5:                        StartActivity(typeof(Demo05Spinner));                        break;                    case 6:                        StartActivity(typeof(Demo06SwitchAndRatingBar));                        break;                    case 7:                        StartActivity(typeof(Demo07PopupMenu));                        break;                    case 8:                        StartActivity(typeof(Demo08Gallery));                        break;                    case 9:                        StartActivity(typeof(Demo09SeekBar));                        break;                }            };        }    }}

注意:直接定义字符串数组这种方式虽然用法最简单易理解,但并不是最好的办法,下一章再介绍比这个稍微好一点的办法(但仍然不是最好的办法,别着急,一步一步来,要不你没有比较怎么知道哪个好哪个坏呢)。

4、清单文件(AndroidManifest.xml)

在这个文件中只添加了一条内容:设置应用到所有页面的公用主题。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ch05demos.ch05demos" android:versionCode="1" android:versionName="1.0">    <uses-sdk />    <application android:label="ch05demos"       android:theme="@android:style/Theme.DeviceDefault.Light"></application></manifest>

从下一节开始,将分别介绍如何实现各个示例,以及这些示例涉及的相关概念。

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 读取android手机流量信息
  4. android 使用html5作布局文件: webview跟javascript交互
  5. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  6. android“设置”里的版本号
  7. Android开发环境搭建
  8. Android(安卓)Resource介绍和使用
  9. 2014.01.21 ——— android 关联android-support源码

随机推荐

  1. Android(安卓)Design Support Library(3)-
  2. Android L之 VectorDrawable
  3. Android通知栏的变化
  4. Android ThreadLocal 源码分析
  5. [Android] View和ViewGroup事件分发机制
  6. Android中包含多个Activity的应用退出按
  7. 通用代码库之listview和GridView
  8. Android:WebService
  9. 混淆Android JAR包的方法
  10. android 判断手机号是否格式正确