1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android】Kotlinの画面遷移

Last updated at Posted at 2020-09-13

Android Kotlin画面遷移方法について調べてみた。

###Manifestへの追加

AndroidManifest.xml
 <activity android:name=".MoveApp" android:label="Move" ></activity>

###xml画面作成
デザインモードで作成した物をコード化すると以下になる。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/moveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="移動する"
        tools:layout_editor_absoluteX="154dp"
        tools:layout_editor_absoluteY="426dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
app_move.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/returnButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="266dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

###画面遷移コード

Intentが画面遷移を行うコードとなる。

MainActivity.kt
package com.example.moveapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity:AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setScreenMoveApp()
    }

    private fun setScreenMoveApp(){
        setContentView(R.layout.activity_main)
        setScreenMain()
    }

    private fun setScreenMain() {
        setContentView(R.layout.activity_main)
        moveButton.setOnClickListener{
            val intent = Intent(application, MoveApp::class.java)
            startActivity(intent)
        }
    }
}
MoveApp.kt
package com.example.moveapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.app_move.*


class MoveApp:AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.app_move)
        setScreenMoveApp()
    }

    private fun setScreenMoveApp(){
        setContentView(R.layout.app_move)
        setScreenMain()
    }

    private fun setScreenMain(){
        setContentView(R.layout.app_move)
        returnButton.setOnClickListener{
            val intent = Intent(application, MainActivity::class.java)
            startActivity(intent)
        }
    }
}

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?