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 进行引用。

相关推荐
Coffeeee6 小时前
如何使用Glide和Coil加载WebP动图
android·kotlin·glide
SimonKing6 小时前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户298698530146 小时前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端
Kapaseker7 小时前
5 分钟搞懂 Kotlin DSL
android·kotlin
恋猫de小郭7 小时前
AI Agent 开发究竟是啥?如何用 AI 开发 Agent ?深入浅出给你一套概念
android·前端·ai编程
黄林晴8 小时前
Android 17 正式发布!target 37 一大批旧代码直接不能用了
android
Carson带你学Android8 小时前
Android 17 正式发布:AI 终于成了系统能力
android·前端·ai编程
三少爷的鞋8 小时前
当 UseCase 开始长期监听,它可能已经不是 UseCase 了
android
恋猫de小郭21 小时前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭21 小时前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter