1
4

More than 3 years have passed since last update.

[ Android Studio ] 画面遷移

Last updated at Posted at 2019-10-17

前回は画面変更を行ったので、
今回は画面遷移に挑戦する。(ボタン押下による遷移)

Intent

画面遷移には複数のやり方があるが、今回はIntentを扱う。

用意するファイル

以下の四つである。

・MainActivity.java
・activity_main.xml
・SubActivity1.java
・activity_sub1.xml

また変更するファイルを以下に示す。

・AndroidManifest.xml

クリックイベント

以下のコードをクリックイベント内に書き、画面遷移プログラムを実装できる。

        startActivity(new Intent(this, SubActivity1.class));

また遷移先画面のボタンのクリックイベントは次のように実装する。
これによりアクティビティを終了させることができる。

        finish();

・AndroidManifest.xmlには新しい画面の情報を加えておく必要があるので、
以下のコードを追加する。

        <activity android:name=".SubActivity1"
            android:label="string/app_name">
        </activity>

サンプルプログラム

・MainActivity.java

import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.toSub1Button);
        button.setOnClickListener((View v) -> {
            startActivity(new Intent(this, SubActivity1.class));
        });
    }
}

・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/toSub1Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="386dp"
        android:text="toSub1Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></Button>

</androidx.constraintlayout.widget.ConstraintLayout>

・SubActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SubActivity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub1);

        Button returnButton = findViewById(R.id.returnButton);

        returnButton.setOnClickListener((View view2)->{
            finish();
        });
    }
}

・activity_sub1.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=".SubActivity1">

    <Button
        android:id="@+id/returnButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="164dp"
        android:layout_marginTop="380dp"
        android:text="returnButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></Button>

</androidx.constraintlayout.widget.ConstraintLayout>

・AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.applicationtransration">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SubActivity1"
            android:label="string/app_name">
        </activity>
    </application>

</manifest>

補足

Intentはデータを渡すputExtra()といったメソッドを持っている。
今回は実装していないが、今後試してみたい。

またIntent以外にもFragmentをつかって画面遷移ができる。
複数の画面を遷移できるので、試したい。

1
4
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
4