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

相关推荐
阿巴斯甜18 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker19 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952720 小时前
Andorid Google 登录接入文档
android
黄林晴21 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇2 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android