Activity是一个应用中的组件,它为用户提供一个可视的界面,方便用户操作,比如说拔打电话、照相、发邮件或者是浏览地图等。每个activity会提供一个可视的窗口,一般情况下这个窗口会覆盖整个屏幕,但在某此情况下也会出现一些比屏幕小的窗口飘浮在另外一相窗口上面。类比Windows当中的概念,Activity相应于一个Dialog(MFC)或者是Form(C#),它为用户提供一个可视的界面。
下面是摘自android API的,关于android.app.Activity的介绍:
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(int). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with android.R.attr#windowIsFloating set) or embedded inside of another activity (using ActivityGroup).
关于Activity的生命周期,是非常重要的,这些将在以后的文章中介绍到。
今天我们将初步接触到的其实是一个小把戏,说它小把戏,其实是因为它只是单纯的Activity配置之间的切换,在实际中,这种切换是没有多大的意义的,而真正的Activity的切换,比这个要复杂。
但是要理解和运用Activity之间的切换,我觉得,先接触本文的这种方式将会很有裨益。
进入正题,本例中的代码运行效果如下图:
本例模拟的是一个安装程序,点击第一幅画面的“下一步”,进入的第二幅画面,为了以示区别,我们将第二幅画面的背景颜色变成了白色。点击“上一步”,又会回到上一个画面,在点击按钮的同时,Activity看上去在不断地切换着。
首先,我们在strings.xml中定义本例中所需要用到的字符串。
<string name="hello">欢迎安装本程序,本程序采用安卓技术,保证\n更炫的体验, 更稳定的操作环境,更优质的服务。\n\n\n请点击“下一步”继续\n\n</string> <string name="nextMessage">对不起,程序未开发完全,请等待新版本的发布\n\n\n点击"上一步"返回\n\n</string> <string name="app_name">AndroidWithViewChange</string> <string name="nextButtonText">下一步</string> <string name="preButtonText">上一步</string>
在第一篇笔记中,我们介绍到,layout文件夹中存放的xml都是和布局UI相关的,其实系统默认生成的main.xml就对应了我们程序默认的一个Activity配置,
setContentView(R.layout.main);
这句将main.xml中设置的配置,和Activity的子类相关联。
在main.xml中,我们定义一个TextView和Button,配置如下:
<?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" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/nextButton" android:layout_height="wrap_content" android:layout_width="60px" android:text="@string/nextButtonText" /> </LinearLayout>
在main.xml对应的文件夹下,新建另外一个xml文件,命名:mylayout.xml,
定义一个TextView和一个Button
其配置如下:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/nextMessage" /> <Button android:id="@+id/preButton" android:layout_height="wrap_content" android:layout_width="60px" android:text="@string/preButtonText" /> </LinearLayout>
此处,我们系统生成的Activity的子类是以main.xml的配置来生成Activity的,而mylayout的配置是另外要重新绘制布局的配置文件。
其代码如下所示:
package cn.irving;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Test extends Activity { private Button nextButton ; private Button preButton ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //载入main.xml setContentView(R.layout.main); //以findById的方式找到nextButton对象,并且绑定事件 nextButton = (Button)findViewById(R.id.nextButton); nextButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { goNextPage(); } }); } //跳转到下一页 private void goNextPage(){ setContentView(R.layout.mylayout); preButton = (Button)findViewById(R.id.preButton); preButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { goPrePage(); } }); } //跳转到上一页 private void goPrePage(){ setContentView(R.layout.main); nextButton = (Button)findViewById(R.id.nextButton); nextButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { goNextPage(); } }); } }
从上面的分析,我们可以发现,其实并不是在Activity之间进行的切换,我们只有一个Activity类,但是却有两套不同的Activity的配置文件,在点击按钮的同时,给Activity设置上不同的ContentView,这样就造成了不同Activity切换的现象。
下一节我们将真正体会不同的Activity的切换,仅将此文作为下一节的引子吧。