用kotlin 开发一个简单的多页面跳转

本文介绍一个简单的安卓应用的页面跳转例子,用的是kotlin。

运行时主页面是一个hello 和Jump 按钮,你按一下jump 按钮就转到 从页面,只是标识从页面。

开始建立一个简单工程,名为hello, 选择的是Empty views Activity,然后修改下面5个文件。

那个package 这一行不要改,是你建立工程时定义的。就是kotlin 的第一行定义你的包,要与你建立工程的一致。

一个页面都包括页面和kotlin 代码文件。

主页面是一个Textview 和Button。前者表示主页面,后者就是一个跳转按钮。

代码也就是处理跳转。

先上主页面的页面文件,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="Hello Leon1!"
        />
      <Button
            android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
            android:text="Jump">

      </Button>
</LinearLayout>

然后是主页面的kotlin代码文件; MainActivity.kt

Kotlin 复制代码
package com.liwensoft.hello

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val copy: Button = findViewById(R.id.button)
        copy.setOnClickListener {
            OnClick()
        }
        Log.i("CAMERAACTIVITY", "ACTIVITY1 onCreate")
    }

    private fun OnClick() {
        Log.i("button","click")
        val intent = Intent(this, MainActivity2::class.java)
        startActivity(intent)
        Log.i("button","button finish click")

    }
}

第2页面就很简单,只是显示页面标识

页面文件 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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Activity2!"
        />

</LinearLayout>

代码文件 MainActivity2.kt:

Kotlin 复制代码
package com.liwensoft.hello

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity2: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("2ACTIVITY", "activity 2 onCreate")
        setContentView(R.layout.activity_main2)

    }
}

还有一个地方要修改,那就是AndroidManifest.xml

主要的是注册MainActivity2.xml 我开始没有注册,结果总是跳转不了。

XML 复制代码
<?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.Hello"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>
    </application>

</manifest>

应该只是修改上面5个文件。关键点是主页面的跳转按钮事件,内容就一点点:

XML 复制代码
val intent = Intent(this, MainActivity2::class.java)
        startActivity(intent)

然后就是要注册MainActivity2

列出5个文件只是你走迷宫了。当然跳转还可以带参数,返回也可以返回参数,这里就没有做,为了简单。跳转后你再查看下。

相关推荐
DogDaoDao1 天前
【第 05 篇】Python的字典与集合
开发语言·python·集合·字典
百度搜知知学社1 天前
一键装裱照片,相框APP内置滤镜与贴纸编辑器
android·编辑器·滤镜·图片编辑·贴纸·相框
兰令水1 天前
leecodecode【单调栈】【2026.6.12打卡-java版本】
java·开发语言·算法
leagsoft_10031 天前
零信任选型五刀法——零信任怎么选?五个问题,五条红线
开发语言·php
AI人工智能+电脑小能手1 天前
【大白话说Java面试题 第112题】【并发篇】第12题:AQS 中节点的入队时机有哪些?
java·开发语言·面试
IT WorryFree1 天前
Zabbix 7.4 API 可同步全量参数清单(同步第三方系统专用)
java·开发语言·zabbix
码云骑士1 天前
06-Python装饰器从入门到源码(上)-闭包与自由变量
开发语言·python
AFinalStone1 天前
Android12 U盘插拔链路源码全解析(四):Framework层(上) —— UsbHostManager
android·frameworks
码云骑士1 天前
10-Python运行时内存模型-栈帧-堆-引用计数-GC分代回收的全景图
开发语言·python
智码看视界1 天前
老梁聊全栈系列 JavaScript语言本质:从原型链到异步编程的深度解析
开发语言·javascript·全栈·javascript核心