Android Studio入门——页面跳转

1.工程目录

2.MainActivity

java 复制代码
package com.example.demo01;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.修改文本
        TextView tv = findViewById(R.id.tv);
        tv.setText("你好! "); //再次设置activity_main.xml  中  android:text="页面1"
        //2.实现跳转
        View button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });


    }
}

3.MainActivity2

MainActivity2为右键layout------new------Activity------Empty Views Activity生成,也可手动创建。

java 复制代码
package com.example.demo01;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

4.activity_main.xml

XML 复制代码
<?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"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面1" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"/>

</LinearLayout>

5.activity_main2.xml

XML 复制代码
<?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"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面2" />

</LinearLayout>

6.启动项目

7.补充

text可以这样写,将文本写到strings.xml中,在activity_main2.xml中使用@string/文本的name 进行引用。

相关推荐
考虑考虑3 小时前
JDK26中的List.ofLazy()
java·后端·java ee
小蒜学长3 小时前
基于SpringBoot的公寓报修管理系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·公寓报修管理系统·多角色协同
for_ever_love__4 小时前
MySQL 主从复制讲透:binlog 原理、搭建步骤与读写分离
java·数据库·mysql·binlog·读写分离·原理·主从复制
alexhilton6 小时前
模块化Jetpack Compose架构
android·kotlin·android jetpack
小π军6 小时前
操作系统面试题
java·linux·服务器
倔强的石头1066 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
Shaoshing7 小时前
ThreadLocal
java·jvm·threadlocal
写后端的胖头鱼7 小时前
一文详解CAS
java·jvm·spring·cas·乐观锁·自旋
鱼宵7 小时前
Spring AI 提示词模板:{变量} 参数化 + few-shot,一条提示词反复用
java·人工智能·spring·few-shot·提示词工程·springai