分類:C#、Android、VS2015;
創建日期:2016-02-08
在Android應用中,常用的對話框有:Toast、AlertDialog、ProgressDialog、時間選擇對話框、日期選擇對話框等。這一章主要介紹這些常用對話框的基本用法。
本章源程序共有4個示例,這些示例都在同一個項目中。
項目名:ch06demos
項目模板:Blank App(Android)
1、運行截圖
主界面運行截圖如下:
點擊每行的示例項,即進入對應示例的頁面。
2、主界面(Main.axml)
將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、在AndroidManifest.xml文件中添加使用的主題
設置應用到所有頁面的主題。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ch06demos.ch06demos" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" />
<application android:label="ch06demos"
android:theme="@android:style/Theme.DeviceDefault.Light">
</application>
</manifest>
4、主界面對應的活動文件(MainActivity.cs)
本章示例全部完成後MainActivity.cs的代碼如下:
using System.Collections.Generic;
using Android.App;
using Android.Widget;
using Android.OS;
using ch06demos.SrcActivity;
namespace ch06demos
{
[Activity(Label = "ch06demos", 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[]
{
"Demo01:Toast",
"Demo02:AlertDialog",
"Demo03:ProgressDialog",
"Demo04:DatePicker、TimePicker"
};
ListView listView1 = FindViewById<ListView>(Resource.Id.listView1);
listView1.Adapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleListItem2, items);
listView1.ItemClick += (s, e) =>
{
int index = e.Position + 1;
switch (index)
{
case 1:
StartActivity(typeof(Demo01Toast));
break;
case 2:
StartActivity(typeof(Demo02AlertDialog));
break;
case 3:
StartActivity(typeof(Demo03ProgressDialog));
break;
case 4:
StartActivity(typeof(Demo04DatePickerDialog));
break;
}
};
}
}
}