U3

一个项目里面可以有多个Activity

AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

在谁的里面就运行谁


复制代码
在两个Activity 里切换

package com.example.chapter31;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("Activity1","onCreate");
        Button  button =(Button)findViewById(R.id.button);
        button.setOnClickListener (new View.OnClickListener(){
            @Override
            public void onClick (View view){
                Intent intent = new Intent (MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }});
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("Activity1","onStart()");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("Activity1","onRestart()");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("Activity1","onResume()");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i("Activity1","onPause()");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i("Activity1","onStop()");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("Activity1","onDestroy()");
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<Button
    android:id = "@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight = "1"
    android:text = "跳转到第二个Activity"  />



</LinearLayout>



package com.example.chapter31;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        Log.i("Activity1","onCreate");
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener (new View.OnClickListener(){
            @Override
            public void onClick (View view){
                Intent intent = new Intent (SecondActivity.this,MainActivity.class);
从2到1
                startActivity(intent);
            }});
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("Activity2","onStart()");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("Activity2","onRestart()");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("Activity2","onResume()");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i("Activity2","onPause()");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i("Activity2","onStop()");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("Activity2","onDestroy()");
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SecondActivity">
    <Button
        android:id = "@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight = "1"
        android:text = "跳转到Activity1"  />



</LinearLayout>

别忘了在AndroidManifest 里面注册
复制代码

 

posted on   cltt  阅读(276)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示