用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个文件只是你走迷宫了。当然跳转还可以带参数,返回也可以返回参数,这里就没有做,为了简单。跳转后你再查看下。

相关推荐
传奇开心果编程5 小时前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
墨狂之逸才5 小时前
键盘一弹,扫码枪就失灵:Android 硬件扫码与软键盘冲突的根因与解法
android
山楂树の5 小时前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
xurileidian6 小时前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
FRing037 小时前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
syf0611137 小时前
Java Math 类详解:常用数学运算方法全解析
java·开发语言
是main不是漫7 小时前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
geovindu8 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
妖星9 小时前
Android 13 WindowContainer 树是如何一步一步创建出来的
android
星夜离尘569 小时前
MATLAB:两路读数平均相差 8 mV,换位能分出传感器偏差和通道偏差吗?
开发语言·matlab