概括:
界面跳转通过Intent进行跳转:
分为隐式Intent 显示Intent
一显示Intent Actiuvity类名方式
Intent i=new Intent(MainActivity.this,Aty1.class);
startActivity(intent);
二.隐式Intent
需要在AndroidManifest.xml文件中配置:action category
action名可以随便定义, category可以默认也可以自己定义,默认的是"android.intent.category.DEFAULT"通过Intent发送可以不用定义category,若是自己命名的话需要通过intent.addCategory();
1. category默认
Intent intent=new Intent("TART");
startActivity(intent);
AndroidManifest.xml
<intent-filter>
<action android:name="TART"/>
<category android:name="android.intent.category.DEFAULT"/> //这种名字是系统定义
</intent-filter>
2. category自己定义
Intent intent=new Intent("TART");
intent.addCategory(“xxx”)
startActivity(intent);
AndroidManifest.xml
<intent-filter>
<action android:name="TART"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="xxx"/> //自己定义
</intent-filter>
Button设定规则:
定义 查找 设置监听模式
Intent设定规则:
定义 绑定另一个页面 启动另一个页面
关闭页面:
finish();
一. 创建MainActivity界面(内部有Button控件),创建Aty1界面(内部有Button控件),创建layout.xml文件,通过点击Button控件,在两个界面之间跳转.
MainActivity文件
1.在MainActivity文件里面定义Activity的七种状态:
onStart() onPause() onResume() onStop() onDestroy() onRestart()
2.定义private Button btnStartAty1;,设定点击监听状态
3.定义 Intent 绑定本界面和Aty1界面
4.点击按钮后 startActivity(i);显示Aty1界面。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnStartAty1; //定义button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//将activity_main.xml界面绑定
//按钮btnStartAty1
btnStartAty1=(Button)findViewById(R.id.btnStartAty1);
//查找activity_main.xml 中id=btnStartAty1的组件 组件需要强制转换(Button类是View类的子类)
btnStartAty1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { //点击按钮后会调用
Intent i=new Intent(MainActivity.this,Aty1.class);//MainActivity Aty1.class
startActivity(i); //启动Aty1界面
}
});//给钮btnStartAty1安装事件监听器
System.out.println("MainActivity :----onCreate----");
}
protected void onStart() {
super.onStart();
System.out.println("MainActivity :----onStart----");
}
protected void onPause() {
super.onPause();
System.out.println("MainActivity :----onPause----");
}
protected void onResume() {
super.onResume();
System.out.println("MainActivity :----onResume----");
}
protected void onStop() {
super.onStop();
System.out.println("MainActivity :----onStop----");
}
protected void onDestroy() {
super.onDestroy();
System.out.println("MainActivity :----onDestroy----");
}
protected void onRestart() {
super.onRestart();
System.out.println("MainActivity :----onRestart----");
}
}
Aty1文件
1.定义private Button btnClose; 用来跳转到MainActivity界面,
2.当点击按钮后使用 finish();关闭自己
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Aty1 extends Activity {
private Button btnClose; //定义
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);//将layout 见面添加进来
btnClose=(Button)findViewById(R.id.btnClose);//在layout.xml 界面中查找id为btnClose的组件
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//当点击
finish();//关闭自己
}
});
}
}
activity_main.xml文件
定义 <TextView <Button 两个组件,
<?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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnStartAty1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Aty1"
tools:layout_editor_absoluteX="47dp"
tools:layout_editor_absoluteY="101dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout.xml文件
定义 <TextView <Button组件
<?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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Aty1界面" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="关闭自己" />
</LinearLayout>
里面定义activity Aty1
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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="Aty1"></activity>
</application>
</manifest>
运行效果: