代码改变世界

Xamarin.Android多界面

2014-07-09 09:54  y-z-f  阅读(11507)  评论(9编辑  收藏  举报

一、准备

开始学习本教程前必须先完成该教程http://www.cnblogs.com/yaozhenfa/p/xamarin_android_quickstart.html 否则将无法继续。

 

二、界面

1.打开Resources/layout/Main.axml文件,并在Call Button下方继续加入一个按钮,并设置其id为@+id/CallHistoryButton同时设置Text为@string/callHistory(这个其实是一个字符串资源的标识符,后面我们会添加该资源):

 

三、资源

1.打开Resources/values/Strings.xml文件

 

2.并在其中加入一个name为callHistory的字符串资源:

 

3.回到Main.axml可以看到最后一个button显示的字符串变掉了:

 

4.之前的Call button是通过代码的方式禁用的,这次我们将CallHistory Button通过属性该改变:

 

可以看到按钮被禁用了:

 

四、代码

1.右击项目,新建一个名为CallHistoryActivity的活动:

 

2.打开刚才新建的活动,修改该活动的标题名称,继承的类并显示传递过来的字符串数组:

 1 namespace Phoneword_Droid
 2 {
 3     [Activity(Label = "@string/callHistory")]
 4     public class CallHistoryActivity : ListActivity
 5     {
 6         protected override void OnCreate(Bundle bundle)
 7         {
 8             base.OnCreate(bundle);
 9             //从意图中获取传递过来的参数
10             var phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];
11 
12             //将字符串数组显示到列表控件中(因为继承的是ListActivity所以整个视图就是一个列表)
13             this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);
14 
15             //关于ArrayAdapter的第二个参数,其实就是指定列表中每个项的视图,后面我们会通过自定义的方式控制列表的项
16         }
17     }
18 }
View Code

 

 

3.回到MainActivity.cs中,既然要显示历史记录,那么自然就必须要能够保存所以我们需要定义一个变量:

1     [Activity(Label = "Phoneword_Droid", MainLauncher = true, Icon = "@drawable/icon")]
2     public class MainActivity : Activity
3     {
4         static readonly List<string> phoneNumbers = new List<string>();
View Code

 

 

4.然后还要为callHistoryButton绑定监听事件,以便打开另一个活动(在OnCreate后面继续追加):

1             Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton);
2             callHistoryButton.Click += (e, t) =>
3             {
4                 //指定意图需要打开的活动
5                 var intent = new Intent(this, typeof(CallHistoryActivity));
6                 //设置意图传递的参数
7                 intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
8                 StartActivity(intent);
9             };
View Code

 

 

5.我们缺少一个添加历史记录的方法,这里我们应该将其放入对话框的Call方法中,这样只要拨打了的电话才会进入到历史记录中:

 1                 //拨打按钮
 2                 callDialog.SetNeutralButton("Call", delegate
 3                 {
 4                     //将电话加入到历史记录列表中
 5                     phoneNumbers.Add(translatedNumber);
 6 
 7                     //如果callHistoryButton的定义在这段代码后面将会出错,所以我们这个时候需要将
 8                     //Button callHistoryButton = FindViewById<Button>(Resource.Id.CallHistoryButton); 代码提前
 9                     callHistoryButton.Enabled = true;
10 
11                     //使用意图拨打电话
12                     var callIntent = new Intent(Intent.ActionCall);
13 
14                     //将需要拨打的电话设置为意图的参数
15                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
16                     
17                     StartActivity(callIntent);
18                 });
View Code

 

 

五、运行