Android activity相当于web的页面,有web开发经验的朋友这一点就比较清楚,在这里我就不多讲了。
我就简单的讲一下activity直接的跳转,其实个人感觉Android开发跟web开发有很多相似的地方,在web中很多东西都是通过xml来配置的,但是web中很多页面的请求事件都是通过js来控制的,但是Android是通过自己的事件进行操作的有点类似swing中的事件。
在android project中有几个配置文件是特别重要的,string.xml主要是通过key-value方式配置一些参数,还有一个是layout文件夹,默认情况是自动生成一个main.xml。有swing开发经验的人一看这个文件夹就应该知道他的作用,它主要是用来存放页面的布局文件的,这个目录就是用来布局每一个activity,类似web中的css样式,还有一个就是AndroidManifest.xml,这个xml文件主要是用来注册activity用的,接触过spring的人应该都知道spring中的bean也是通过xml进行注册的,如果用eclipse开发会自动生成一个R.java类,这个类主要是用来维护一些信息,比如添加参数或者布局时候R.java里面会相应找到相关信息,这个就不多讲了。
首先在string.xml定义一些参数:
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
string
name
=
"hello"
>Activity之间切换</
string
>
<
string
name
=
"app_name"
>Activity</
string
>
<
string
name
=
"first"
>第一个Activity</
string
>
<
string
name
=
"next"
>下一个Activity</
string
>
<
string
name
=
"firstBt"
>上页</
string
>
<
string
name
=
"nextBt"
>下页</
string
>
</
resources
>
|
默认情况下是找到main.xml中的布局文件,在这个文件中添加一个TextView和Button。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
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/first"
/>
<
Button
android:id
=
"@+id/fistButton"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/nextBt"
/>
</
LinearLayout
>
|
以下是创建第一个activity类,因为Activity类是父类所以我们定义自己的activity时候必须继承这个父类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
ChangeActivity
extends
Activity {
/** Called when the activity is first created. */
private
Button fistButton;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
fistButton = (Button) findViewById(R.id.fistButton);
fistButton.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
Intent it =
new
Intent();
it.setClass(ChangeActivity.
this
, NextActivity.
class
);
startActivity(it);
ChangeActivity.
this
.finish();
}
});
}
}
|
单击第一个button时候就触发一个事件,通过Intent来启动activity进入下一个activity中的。
1
|
it.setClass(ChangeActivity.
this
, NextActivity.
class
);
|
就是从第一个activity跳转到第二个activity。
接着创建第二个activity布局文件mylayout.xml文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
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/next"
/>
<
Button
android:id
=
"@+id/nextButton"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/firstBt"
/>
</
LinearLayout
>
|
在这里也是定义一个TextView和一个Button,创建第二个activity类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
NextActivity
extends
Activity {
private
Button nextButton;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
Intent intent =
new
Intent();
intent.setClass(NextActivity.
this
, ChangeActivity.
class
);
startActivity(intent);
NextActivity.
this
.finish();
}
});
}
}
|
在这里再添加一个button事件,触发这个事件时候就跳转会第一个activity中,这个就是一个简单的activity之间的跳转。
以下是执行结果图: