第一周作业:简单实现页面的跳转---点击按钮实现跳转
代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Home1"
tools:targetApi="31">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个页面"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第二个页面"
android:onClick="jump"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_second:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个页面"
android:textSize="30sp"/>
</LinearLayout>
MainActivity:
package com.example.home1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("MainActivity------>onCreate");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("MainActivity------>onStart");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("MainActivity------>onStop");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("MainActivity------>onPause");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("MainActivity------>onResume");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("MainActivity------>onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("MainActivity------>onDestroy");
}
public void jump(View view){
Intent intent=new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
SecondActivity:
package com.example.home1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
System.out.println("MainActivity------>onCreate");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("SecondActivity------>onStart");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("SecondActivity------>onStop");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("SecondActivity------>onPause");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("SecondActivity------>onResume");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("SecondActivity------>onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("SecondActivity------>onDestroy");
}
}