1,在activity_main.xml中添加一个按钮
内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:id = "@+id/container_1"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "com.example.activity_tiaozhuan.MainActivity"
tools:ignore = "MergeRootFrame" >
< Button
android:id = "@+id/bt_activity"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_alignParentTop = "true"
android:text = "跳转" />
</ RelativeLayout >
|
2,新建一个跳转的目标页面的xml文件:activity_second.xml,并且添加一个TextView
内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:id = "@+id/container_2"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "com.example.activity_tiaozhuan.MainActivity"
tools:ignore = "MergeRootFrame" >
< TextView
android:id = "@+id/tv_activity"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentTop = "true"
android:text = "第二个activity"
android:textColor = "#ff0000" />
</ RelativeLayout >
|
3,新建一个SecondActivity.java,把MainActivity里的内容复制过去,改下这行内容:
setContentView(R.layout.activity_second);
内容:
1
2
3
4
5
6
7
8
9
10
|
package com.example.activity_tiaozhuan;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class SecondActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
} |
4,在MainActivity里实现页面跳转
内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.example.activity_tiaozhuan; //包名视具体情况自行改动
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt_activity); //查找到按钮bt_activity
bt.setOnClickListener( this ); //给按钮注册点击事件
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_activity:
method(); //按钮是R.id.bt_activity时,调用method()方法来实现点击的具体意图
break ;
}
}
private void method() {
Intent intent = new Intent( this ,SecondActivity. class ); //定义一个跳转到SecondActivity的意图
startActivity(intent); //启动意图intent
}
} |
5,在AndroidMainfest.xml中注册SecondActivity,添加:
<activity
android:name="com.example.activity_tiaozhuan.SecondActivity" ></activity>
内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "com.example.activity_tiaozhuan"
android:versionCode = "1"
android:versionName = "1.0" >
< uses-sdk
android:minSdkVersion = "8"
android:targetSdkVersion = "19" />
< application
android:allowBackup = "true"
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name"
android:theme = "@style/AppTheme" >
< activity
android:name = "com.example.activity_tiaozhuan.MainActivity"
android:label = "@string/app_name" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity
android:name = "com.example.activity_tiaozhuan.SecondActivity" ></ activity >
</ application >
</ manifest >
|
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1409316,如需转载请自行联系原作者