activity跳转例子

纯跳转代码,不包含逻辑

文件目录

两个样式文件

activity_main.xml

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_margin="40dp"
android:background="@drawable/head"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/iv"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:orientation="vertical">

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:ems="10"
android:hint="QQ号/手机号/邮箱"
android:inputType="textPersonName"
android:textColor="#615C5C"
android:textColorHint="#FFC4BABA"
android:textSize="18sp"/>

<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:ems="10"
android:hint="密码"
android:inputType="textPassword"
android:selectAllOnFocus="false"
android:singleLine="false"
android:textColor="#FFC4BABA"
android:textColorHint="#FFC4BABA"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="登录" />

<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="新用户注册" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

sign_success.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="86dp"
android:layout_weight="1"
android:text="登录成功" />
</LinearLayout>

两个activity文件

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
private static final String TGA="ActivityLife";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.e(TGA,"1-onCreate");

ImageView iv=(ImageView)this.findViewById(R.id.iv);

TextView et1= (TextView)this.findViewById(R.id.et1);
TextView et2= (TextView)this.findViewById(R.id.et2);

Button bt1= (Button)this.findViewById(R.id.bt1);
Button bt2= (Button)this.findViewById(R.id.bt2);

bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//显示声明Intent
Intent intent = new Intent(MainActivity.this, signActivity.class);
startActivity(intent);
}
});

}
@Override
protected void onStart(){
super.onStart();
Log.e(TGA,"1-onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.e(TGA,"1-onResume");
}
@Override
protected void onRestart(){
super.onRestart();
Log.e(TGA,"1-onRestart");
}
@Override
protected void onPause(){
super.onPause();
Log.e(TGA,"1-onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.e(TGA,"1-onStop");
}
}

signActivity

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
32
33
34
35
36
37
38
39
package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class signActivity extends AppCompatActivity {
private static final String TGA="ActivityLife";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_success);
}
@Override
protected void onStart(){
super.onStart();
Log.e(TGA,"2-onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.e(TGA,"2-onResume");
}
@Override
protected void onRestart(){
super.onRestart();
Log.e(TGA,"2-onRestart");
}
@Override
protected void onPause(){
super.onPause();
Log.e(TGA,"2-onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.e(TGA,"2-onStop");
}
}

在AndroidManifest中注册两个Activity

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.example.myapplication1">

<dist:module dist:instant="true" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="myqq"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".signActivity"></activity>
</application>
</manifest>

结果

生命周期

打开app

点击登录

返回主页面

关闭

数据传递

要实现传递et1框内输入的数据只需更改两个Activity

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
private static final String TGA="ActivityLife";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.e(TGA,"1-onCreate");

ImageView iv=(ImageView)this.findViewById(R.id.iv);

final TextView et1= (TextView)this.findViewById(R.id.et1);
TextView et2= (TextView)this.findViewById(R.id.et2);

Button bt1= (Button)this.findViewById(R.id.bt1);
Button bt2= (Button)this.findViewById(R.id.bt2);

bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str=et1.getText().toString();
//显示方式声明Intent
Intent intent = new Intent(MainActivity.this, signActivity.class);
intent.putExtra("data",str);
startActivity(intent);
}
});

}
@Override
protected void onStart(){
super.onStart();
Log.e(TGA,"1-onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.e(TGA,"1-onResume");
}
@Override
protected void onRestart(){
super.onRestart();
Log.e(TGA,"1-onRestart");
}
@Override
protected void onPause(){
super.onPause();
Log.e(TGA,"1-onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.e(TGA,"1-onStop");
}
}

signActivity

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.content.Intent;
import android.widget.TextView;

public class signActivity extends AppCompatActivity {
private static final String TGA="ActivityLife";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_success);
//取得启动该Activity的Intent对象
Intent intent =getIntent();
/*取出Intent中附加的数据*/
String str = intent.getStringExtra("data");
TextView tv=(TextView) this.findViewById(R.id.tv);
tv.setText(String.valueOf(str));
}

@Override
protected void onStart(){
super.onStart();
Log.e(TGA,"2-onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.e(TGA,"2-onResume");
}
@Override
protected void onRestart(){
super.onRestart();
Log.e(TGA,"2-onRestart");
}
@Override
protected void onPause(){
super.onPause();
Log.e(TGA,"2-onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.e(TGA,"2-onStop");
}
}

结果