Android中贪吃蛇游戏的学习(五)
贪吃蛇游戏的项目控制文件。AndroidManifest.xml内容如下:
?
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
???? Licensed under the Apache License, Version 2.0 (the "License");
???? you may not use this file except in compliance with the License.
???? You may obtain a copy of the License at
?
????????? http://www.apache.org/licenses/LICENSE-2.0
?
???? Unless required by applicable law or agreed to in writing, software
???? distributed under the License is distributed on an "AS IS" BASIS,
???? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
???? See the License for the specific language governing permissions and
???? limitations under the License.
?Declare the contents of this Android application.? The namespace
???? attribute brings in the Android platform namespace, and the package
???? supplies a unique name for the application.? When writing your
???? own application, the package name must be changed from "com.example.*"
???? to come from a domain that you own or have control over.
???? 直观来看,每个 Activity 通常会负责处理一个萤幕的内容(包含介面、选单、弹出对话框、程式动作等)。
???? 当我们需要从一个萤幕画面切换到另一个萤幕画面的时候,就涉及到了 Activity 切换的动作。 我们可以将
????? Activity 看成 MVC 模式中的 Control。Activity 负责管理 UI(详细的UI细节可以由资源档读入)
????? ,并接受事件触发。以是否需要与其他 Activity 交换资料来区分,Activity 可以粗分为两种类型:
????? 「独立的 Activity」与「相依的 Activity」。
不同类型的 Activity,其动作也不尽相同:
独立的 Activity
?? 独立的 Activity 是不需要从其他地方取得资料的 Activity。只是单纯的从一个萤幕跳到下个萤幕,不涉及
?资料的交换。 从一个独立的 Activity 呼叫另一个独立的 Activity 时,我们只要填好 Intent 的内容和动作,
?使用 startActivity 函式呼叫,即可唤起独立的 Activity。例如前几章中,用作开启特定网页的 Activity。
相依的 Activity
??? 相依的 Activity 是需要与其他 Activity 交换资料的一种 Activity。相依的 Activity 又可再分为单向与双向。
从一个萤幕跳到下个萤幕时,携带资料供下一个萤幕(Activity)使用,就是单向相依的 Activity; 要在两个萤幕之
间切换,萤幕上的资料会因另一个萤幕的操作而改变的,就是双向相依的 Activity。 与独立的 Activity 比起来,相
依的 Activity 变化更加精采。
我们会在后续章节中,对相依的 Activity 做进一步的说明。
独立的 Activity
???? 本章将继续透过改进 BMI 应用程式来讲解 Android 应用程式设计。在这个过程中,我们将使用到独立的
???? Activity。
?? 这章中所做的改动都是为了介绍独立的 Activity,而不是为了让? 程式变得更完整。因此你不妨先将写好的
?? BMI 程式先压缩备份到其他目录中,再随着后面的教学继续探索 Android。
本章的目的是介绍独立的 Activity,会用到两个萤幕,因此除了原本的一个 XML 描述档与一个程式码档桉之外,
我们还会额外再定义一个 XML 描述档与一个程式码档桉,以支援第二个萤幕的动作。
要完成独立的 Activity 的动作,我们要做几件事:
在程式码中建立新 Activity 类别档桉
在清单中新增 Activity 描述
在原 Activity 类别中加入 startActivity 函式
???? -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
??? package="com.easyway.dev.android.snake">
??? <application android:label="Snake on a Phone">
????? <!--
????? 可以定义多个activty对象,但是可以设置是否为主界面
?????? -->
????? <activity android:name="Snake"
??????? android:screenOrientation="portrait"
??????? android:configChanges="keyboardHidden|orientation">
??????????? <intent-filter>
??????????????? <action android:name="android.intent.action.MAIN" />
??????????????? <category android:name="android.intent.category.LAUNCHER" />
??????????? </intent-filter>
??????? </activity>
??? </application>
</manifest>